◆ Script Execution speed test
As mentioned above, we can only optimize the code that affects the speed. The benchmark_timer class and benchmark_iterate class in the benchmark package of pear can be used to conveniently test the speed of script execution. (For installation and configuration of pear, please view relevant information)
First, use the benchmark_iterate class to test the execution time of a function or method of a class in the program.
Benchmark1.php
require_once(
'Benchmark/Iterate.php'
);
$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 the benchmark iterate object $ benchmark, which is used to execute the myfunction for 10 times.
The $ argument variable is passed to myfunction each time. The analysis results run multiple times are saved to $ result, and then obtained using the get () method of the benchmark object. This result is output to the screen using print_r. The following results are usually output:
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 execution of myfunction, the benchmark object tracks the execution time. And calculates the average execution time (the line [mean ). By running the target function multiple times, you can obtain the average running time of the function.
In actual tests, the number of functions should be at least 1000 times, so that objective results can be obtained.