Program | script | speed | As mentioned earlier, we can only optimize if we find code that affects speed. The Benchmark_timer class and Benchmark_iterate class in Pear's benchmark package can be used to easily test the speed of script execution. (For the installation and configuration of pear, please check the relevant information yourself).
first uses the Benchmark_iterate class to test the execution time of a method of a function or class in a program.
benchmark1.php
Require_once (' benchmark/iterate.php ');
$benchmark = new Benchmark_iterate ();
$benchmark->run (' myfunction ', ' test ');
$result = $benchmark->get ();
echo "
"; Print_r ($result); echo "
";
Exit
function MyFunction ($var) {
Do something
Echo ' Hello ';
}
? >
establishes the benchmark iterate object $benchmark, which is used to perform the MyFunction function 10 times.
$argument variables are passed to myfunction each time. The results of the analysis run more than once are stored in $result and then retrieved using the Get () method of the benchmark object. The result is output to the screen with Print_r (). This usually results in the output:
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.0 00070 [9] => 0.000074 [ten] => 0.000072 [mean] => 0.000108 [iterations] => 10)
Each execution of the
MyFunction, the benchmark object tracks execution time. And the average execution time ([mean] line) is computed. By running the objective function multiple times, you can get the average elapsed time of the function.
in the actual test, the number of functions should be at least 1000 times, so that more objective results can be obtained.