|
這是php性能優(yōu)化系列第二期,如何使用PEAR工具類Benchmark逐行獲取代碼或函數(shù)的執(zhí)行時(shí)間。
工欲善其事,必先利其器!
如何安裝PEAR和Benchmark
請(qǐng)參考php性能優(yōu)化系列第一期 [php性能優(yōu)化準(zhǔn)備篇圖解PEAR安裝]
Benchmark工具類包說(shuō)明
直接下載:http://pear.php.NET/package/Benchmark/download
Benchmark工具類包共有三個(gè)文件,分別是Timer.php、Iterate.php和Profiler.php,三個(gè)工具類功能相同,只是側(cè)重點(diǎn)不同,都是用于調(diào)試代碼獲取程序的執(zhí)行時(shí)間。
1,Benchmark_Timer類原理與通過(guò)microtime函數(shù)獲取微秒時(shí)間再比較前后兩個(gè)時(shí)間值的差相同。
2,Benchmark_Iterate類用于調(diào)試函數(shù)的平均執(zhí)行時(shí)間。
3,Benchmark_Profiler類用于統(tǒng)計(jì)代碼和函數(shù)的執(zhí)行時(shí)間以及函數(shù)的調(diào)用次數(shù)。
具體使用方法三個(gè)文件內(nèi)都有詳細(xì)的使用實(shí)例。
如何獲取一行或一段代碼的執(zhí)行時(shí)間
1,通常使用microtime函數(shù)獲取代碼前后的微秒時(shí)間數(shù)再比較兩個(gè)值的時(shí)間差,如下
- <?phpfunction microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec);} $time_start = microtime_float(); usleep(100); $time_end = microtime_float();$time = $time_end - $time_start; echo "Did nothing in $time seconds/n";?>
但這種方法很有局限制,不能大范圍的應(yīng)用,而且每次都需要書寫很多代碼,適合于簡(jiǎn)單的調(diào)試。具體請(qǐng)查看php手冊(cè)詳細(xì)說(shuō)明。
2,通過(guò)使用benchmark_Timer類獲取代碼前后執(zhí)行的時(shí)間差,可以同時(shí)獲取N行代碼的執(zhí)行時(shí)間,操作簡(jiǎn)單,只需要增加一個(gè)marker標(biāo)記即可,請(qǐng)看下面Benchmark_Timer類的使用說(shuō)明
如何使用Benchmark_Timer類
Benchmark_Timer類只需要在調(diào)試文件中增加Benchmark_Timer類初始化聲明和marker標(biāo)注,文件尾打印各個(gè)標(biāo)注處的執(zhí)行時(shí)間,實(shí)例如下
- require_once 'Benchmark/Timer.php';$timer = new Benchmark_Timer();$timer->start();$timer->setMarker("marker 01");usleep(1);$timer->setMarker("marker 02");usleep(2);$timer->setMarker("marker 03");usleep(3);$timer->stop();$timer->display();
打印結(jié)果有兩種方法:
一種是表格輸出方式,$timer->display(); 如下圖php-performance-benchmark-timer src="/d/file/itjie/phpjishu/2014-10-22/2c29cb433c3c708d43f00af6d91ae66c.png">
另外一種是手動(dòng)var_dump或print_r打印,$timer->getProfiling();,print_r函數(shù)打印如下圖
- array0 =>array'name' => string 'Start' (length=5)'time' => string '1265942405.31334800' (length=19)'diff' => string '-' (length=1)'total' => string '-' (length=1)1 =>array'name' => string 'marker 01' (length=9)'time' => string '1265942405.31374400' (length=19)'diff' => string '0.000396' (length=8)'total' => string '0.000396' (length=8)2 =>array'name' => string 'marker 02' (length=9)'time' => string '1265942405.31423000' (length=19)'diff' => string '0.000486' (length=8)'total' => string '0.000882' (length=8)3 =>array'name' => string 'marker 03' (length=9)'time' => string '1265942405.31519200' (length=19)'diff' => string '0.000962' (length=8)'total' => string '0.001844' (length=8)4 =>array'name' => string 'Stop' (length=4)'time' => string '1265942405.31623800' (length=19)'diff' => string '0.001046' (length=8)'total' => string '0.002890' (length=8)
結(jié)果說(shuō)明
1,name表示標(biāo)注名稱,如上 包含兩個(gè)特殊標(biāo)注start和stop表示開始和結(jié)束,其次是自定義標(biāo)注 marker 01 marker 02等
2,time表示當(dāng)前的微秒時(shí)間
3,diff表示上一個(gè)標(biāo)記到當(dāng)前標(biāo)記的執(zhí)行時(shí)間,這個(gè)就是我們需要獲取的時(shí)間差,沒(méi)錯(cuò),看的就是這個(gè)值。
4,total表示執(zhí)行到當(dāng)前的整個(gè)時(shí)間
如何使用Benchmark_Iterate類
Benchmark_Iterate類用于調(diào)試函數(shù)執(zhí)行的平均時(shí)間,與Benchmark_Timer類不同在于可以多次調(diào)用同一個(gè)函數(shù)獲取其執(zhí)行時(shí)間的平均值,實(shí)例如下:
- require_once "Benchmark/Iterate.php";$bench = new Benchmark_Iterate;function test($i){ echo $i;}$bench->run(100,"test",10);var_dump($bench->get());
通過(guò)調(diào)用test函數(shù)100次獲取平均執(zhí)行時(shí)間,結(jié)果如下
- array1 => string '0.000486' (length=8)2 => string '0.000466' (length=8).............................(中間省略)99 => string '0.000479' (length=8)100 => string '0.000467' (length=8)'mean' => string '0.000476' (length=8)'iterations' => int 100
結(jié)果說(shuō)明
1,每個(gè)數(shù)字表示每次調(diào)用的時(shí)間
2,mean表示函數(shù)執(zhí)行的平均時(shí)間,如上調(diào)用100次test函數(shù)的平均時(shí)間為0.000476
3,iterations表示函數(shù)調(diào)用的次數(shù)
如何使用Benchmark_Profiler類
Benchmark_Profiler類用于統(tǒng)計(jì)函數(shù)的執(zhí)行次數(shù)和執(zhí)行時(shí)間等,實(shí)例如下:
- require_once 'Benchmark/Profiler.php';$profiler = new Benchmark_Profiler(TRUE);function myFunction() { global $profiler; $profiler->enterSection('myFunction'); //do something $profiler->leaveSection('myFunction'); return;}//do somethingmyFunction();//do more
結(jié)果如下
php-performance-benchmark-profiler src="/d/file/itjie/phpjishu/2014-10-22/ca5877ad458af5510492d0d39f61d975.png">
Benchmark_Profiler類在實(shí)際性能調(diào)試中使用并不多,因?yàn)檫€有比這個(gè)更好的工具,如xDebuger等,因此可直接忽略!
Benchmark 工具類在使用調(diào)試中針對(duì)逐行調(diào)試來(lái)分析程序性能問(wèn)題非常實(shí)用,主要使用Benchmark_Timer類調(diào)試各代碼段的時(shí)間點(diǎn),以通過(guò)獲取執(zhí)行時(shí)間來(lái)優(yōu)化程序提高代碼的性能。這里就不再深入討論,如果在使用的過(guò)程中有什么問(wèn)題歡迎大家一起交流!
如果你發(fā)現(xiàn)這種逐行調(diào)試很累很辛苦,如果你想從整體上把握程序的性能情況,這個(gè)Benchmark類調(diào)試工具就不能滿足你的需求,下期將討論php性能調(diào)試工具xDebuger的安裝與使用。
相關(guān)資料
microtime
(php 3, php 4, php 5)
microtime -- 返回當(dāng)前 Unix 時(shí)間戳和微秒數(shù)
說(shuō)明
mixed microtime ( [bool get_as_float] )
microtime() 當(dāng)前 Unix 時(shí)間戳以及微秒數(shù)。本函數(shù)僅在支持 gettimeofday() 系統(tǒng)調(diào)用的操作系統(tǒng)下可用。
如果調(diào)用時(shí)不帶可選參數(shù),本函數(shù)以 "msec sec" 的格式返回一個(gè)字符串,其中 sec 是自 Unix 紀(jì)元(0:00:00 January 1, 1970 GMT)起到現(xiàn)在的秒數(shù),msec 是微秒部分。字符串的兩部分都是以秒為單位返回的。
如果給出了 get_as_float 參數(shù)并且其值等價(jià)于 TRUE,microtime() 將返回一個(gè)浮點(diǎn)數(shù)。
注意: get_as_float 參數(shù)是 php 5.0.0 新加的。
擴(kuò)展資料
php Benchmark/Timer Class
php Benchmark
Benchmark and Optimize php Script Speed
php技術(shù):PHP性能優(yōu)化工具篇Benchmark類調(diào)試執(zhí)行時(shí)間,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。