PHP program to speed up the exploration of the script execution Speed test

Source: Internet
Author: User
Tags benchmark execution pear
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.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.