This is the second phase of the PHP Performance Tuning series, how to use the Pear tool class benchmark to get code or function execution time by line.
工欲善其事, its prerequisite!
How to install pear and benchmark
refer to the PHP Performance Optimization series Phase I [PHP performance optimization preparation article illustration pear installation]
benchmark Tool Class package description
Direct Download: http://pear.php.net/package/Benchmark/download
Benchmark Tool class package total three files, respectively, timer.php, Iterate.php and profiler.php, three tool classes have the same functionality, but with a different focus, they are used to debug the execution time of the Code acquisition program. The
1,benchmark_timer class principle is the same as the difference between the two time values before and after the microsecond time is obtained by the Microtime function. The
2,benchmark_iterate class is used for the average execution time of a debug function. The
3,benchmark_profiler class is used to count the execution time of code and functions and the number of calls to a function. The
uses a detailed example in three files using a specific method.
How to get the execution time of a line or section of code
1, usually using the Microtime function to get the number of microseconds before and after the code compares the time difference of two values, as follows
<?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";? >
But this method is very limited, not a wide range of applications, and each need to write a lot of code, suitable for simple debugging. Please check the PHP manual for detailed instructions.
2, by using the Benchmark_timer class to get code before and after the time difference, you can get the execution of N-line code, simple operation, only need to add a marker tag, please see the following Benchmark_timer class usage instructions
How to use the Benchmark_timer class
The Benchmark_timer class only needs to add the Benchmark_timer class initialization declaration and the marker annotation in the debug file, and the end of the file prints the execution time of each callout, as the following example
require_once ' benchmark/timer.php '; $timer = new Benchmark_timer (); $timer->start (); $timer->setmarker (" Marker "); Usleep (1); $timer->setmarker (" marker "); Usleep (2); $timer->setmarker (" marker "); Usleep (3); Timer->stop (); $timer->display ();
There are two ways to print results:
One is tabular output , $timer->display (), and the following figure
Another is manual var_dump or Print_ R Prints , $timer->getprofiling (), Print_r function prints the following figure
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 ' (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 ' (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 ' (length= 9) ' time ' => string ' 1265942405.31519200 ' (length=19) ' diff ' => string ' 0.000962 ' (length=8) ' total ' => string ' 0.0 01844 ' (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)
Result description
1,name represents the callout name, including two special callout start and stop representations starting and ending, followed by a custom callout marker marker 02, etc.
2,time represents the current microsecond time
3,diff represents the execution time of the previous tag to the current tag , which is the difference we need to get, yes, that's the value.
4,total represents execution to the current whole time
How to use the Benchmark_iterate class
The average time that the Benchmark_iterate class is used to debug function execution differs from the Benchmark_timer class in that the same function can be called multiple times to obtain an average of its execution time, as follows:
require_once "benchmark/iterate.php"; $bench = new benchmark_iterate;function Test ($i) {echo $i;} $bench->run (M, "test"); Var_dump ($bench->get ());
The average execution time is obtained 100 times by calling the test function, and the result is as follows
array1 => string ' 0.000486 ' (length=8) 2 => string ' 0.000466 ' (length=8) ..... .............. (middle omitted) => string ' 0.000479 ' (length=8) => string ' 0.000467 ' (length=8) '
mean' => string ' 0.000476 ' (length=8) '
iterations' => int
Result description
1, each number represents the time of each call
2,mean represents the average time that a function executes, as the average time of the 100 test function is 0.000476
3,iterations represents the number of function calls
How to use the Benchmark_profiler class
The Benchmark_profiler class is used for statistical function execution times and execution times, as follows:
require_once ' benchmark/profiler.php '; $profiler = new Benchmark_profiler (TRUE); function MyFunction () {Global $PR Ofiler; $profiler->entersection (' MyFunction '); Do something $profiler->leavesection (' MyFunction '); return;} Do Somethingmyfunction ();//do
The results are as follows
The
Benchmark_profiler class is not used in actual performance debugging because there are better tools than this, such as Xdebuger, so you can ignore it directly! The
Benchmark tool class is useful for parsing program performance issues for line-by-row debugging using debugging, primarily by using the Benchmark_timer class to debug a point in time for each code snippet to optimize the program's performance by obtaining execution time. Here is no longer in-depth discussion, if there are any problems in the use of the process to welcome everyone to communicate!
If you find this line-by-row debugging very tiring, if you want to grasp the performance of the program as a whole, this benchmark class debugging tools can not meet your needs, the next issue will discuss the PHP performance debugging tool Xdebuger installation and use.
Related information
microtime
(PHP 3, PHP 4, PHP 5)
Microtime--Returns the current UNIX timestamps and microseconds
Description
Mixed microtime ([bool get_as_float])
Microtime () the current Unix timestamp and microseconds. This function is only available under an operating system that supports Gettimeofday () system calls.
If invoked without an optional argument, this function returns a string in the format "msec sec", where the SEC is the number of seconds since the Unix era (0:00:00 January 1, 1970 GMT), and the msec is the microsecond portion. Both parts of a string are returned in seconds.
Returns a floating-point number if the Get_as_float parameter is given and its value is equivalent to True,microtime ().
Note: The Get_as_float parameter is a new addition to the PHP 5.0.0.
Extended Data
PHP benchmark/timer Class
php Benchmark
Benchmark and Optimize php Script Speed