Home > Article > Backend Development > PHP Program Acceleration Exploration Script Execution Speed Test_PHP Tutorial
As mentioned earlier, only by finding the code that affects speed can we optimize it. The Benchmark_Timer class and Benchmark_Iterate class in PEAR's benchmark package can be used to easily test the speed of script execution. (Please check the relevant information yourself for the installation and configuration of PEAR).
First, use the Benchmark_Iterate class to test the execution time of a certain function or method of a class in the program.
benchmark1.php(as the current mainstream development language)
require_once(Benchmark/Iterate.php(as the current mainstream development language));
$benchmark = new Benchmark_Iterate();
$benchmark->run(10, myFunction,test);
$result = $benchmark->get();
echo "
"; print_r($result); echo "
";
exit;
function myFunction($var) {
// do something
echo Hello ;
}
?>
Create a benchmark Iterate object $benchmark. This object is used to execute the myFunction function 10 times.
The $argument variable is passed to myFunction each time. The analysis results of multiple runs are stored in $result, and then obtained using the get() method of the benchmark object. This result is output to the screen using print_r(). Usually the output is like this:
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
Array
(
[1] => 0.000427 [2] => 0.000079 [ 3] => 0.000072 [4] => 0.000071 [5] => 0.000076 [6] => 0.000070 [7] => 0.000073 [8] => 0.000070 [9] => 0.000074 [10] => 0. 000072 [mean] => 0.000108 [iterations] => 10)
Every time myFunction is executed, the benchmark object will track the execution time. And the average execution time ([mean] line) will be calculated. By running the target function multiple times, you can get the average running time of the function.
In actual testing, the number of functions should be at least 1,000 times, so that more objective results can be obtained.