这是PHP性能优化系列第二期,如何使用PEAR工具类Benchmark逐行获取代码或函数的执行时间。
工欲善其事,必先利其器!
如何安装PEAR和Benchmark
请参考PHP性能优化系列第一期 [PHP性能优化准备篇图解PEAR安装]
Benchmark工具类包说明
直接下载:http://pear.php.net/package/Benchmark/download
Benchmark工具类包共有三个文件,分别是Timer.php、Iterate.php和Profiler.php,三个工具类功能相同,只是侧重点不同,都是用于调试代码获取程序的执行时间。
1,Benchmark_Timer类原理与通过microtime函数获取微秒时间再比较前后两个时间值的差相同。
2,Benchmark_Iterate类用于调试函数的平均执行时间。
3,Benchmark_Profiler类用于统计代码和函数的执行时间以及函数的调用次数。
具体使用方法三个文件内都有详细的使用实例。
如何获取一行或一段代码的执行时间
1,通常使用microtime函数获取代码前后的微秒时间数再比较两个值的时间差,如下
但这种方法很有局限制,不能大范围的应用,而且每次都需要书写很多代码,适合于简单的调试。具体请查看PHP手册详细说明。
2,通过使用benchmark_Timer类获取代码前后执行的时间差,可以同时获取N行代码的执行时间,操作简单,只需要增加一个marker标记即可,请看下面Benchmark_Timer类的使用说明
如何使用Benchmark_Timer类
Benchmark_Timer类只需要在调试文件中增加Benchmark_Timer类初始化声明和marker标注,文件尾打印各个标注处的执行时间,实例如下
打印结果有两种方法:
一种是表格输出方式,$timer->display(); 如下图
另外一种是手动var_dump或print_r打印,$timer->getProfiling();,print_r函数打印如下图
结果说明
1,name表示标注名称,如上 包含两个特殊标注start和stop表示开始和结束,其次是自定义标注 marker 01 marker 02等
2,time表示当前的微秒时间
3,diff表示上一个标记到当前标记的执行时间,这个就是我们需要获取的时间差,没错,看的就是这个值。
4,total表示执行到当前的整个时间
如何使用Benchmark_Iterate类
Benchmark_Iterate类用于调试函数执行的平均时间,与Benchmark_Timer类不同在于可以多次调用同一个函数获取其执行时间的平均值,实例如下:
Get the average execution time by calling the test function 100 times. The results are as follows
Result Description
1, each number represents the time of each call
2, mean represents the average time of function execution, the average time of calling the test function 100 times as above is 0.000476
3, iterations represents the number of function calls
How to use the Benchmark_Profiler class
The Benchmark_Profiler class is used to count the number of executions of a function and execution time, etc. Examples are as follows:
The results are as follows
The Benchmark_Profiler class is not used much in actual performance debugging, because there are better tools than this, such as xDebuger, etc., so it can be ignored directly!
The Benchmark tool class is very useful for analyzing program performance issues during line-by-line debugging during debugging. The Benchmark_Timer class is mainly used to debug the time points of each code segment to optimize the program and improve the performance of the code by obtaining the execution time. I won’t discuss it in depth here. If you have any questions during use, please feel free to share it with us!
If you find this line-by-line debugging very tiring and hard, and if you want to grasp the performance of the program as a whole, this Benchmark debugging tool cannot meet your needs. We will discuss PHP performance debugging tools in the next issue Installation and use of xDebuger.
Related information
microtime
(PHP 3, PHP 4, PHP 5)
microtime -- Returns the current Unix timestamp and microseconds
Description
mixed microtime ( [bool get_as_float] )
microtime() Returns the current Unix timestamp and microseconds Number of seconds. This function is only available under operating systems that support the gettimeofday() system call.
If called without optional parameters, this function returns a string in the format "msec sec", where sec is the number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT) , msec is the microsecond part. Both parts of the string are returned in seconds.
If the get_as_float parameter is given and its value is equivalent to TRUE, microtime() will return a floating point number.
Note: The get_as_float parameter is newly added in PHP 5.0.0.
Extended information
PHP Benchmark/Timer Class
PHP Benchmark
Benchmark and Optimize PHP Script Speed