PHP Performance Tuning Tool benchmark class Debug execution time _php tutorial

Source: Internet
Author: User
Tags benchmark pear php performance tuning usleep
This is the second phase of the PHP performance Optimization series, how to use the Pear tool class benchmark to get the execution time of the code or function row by line.

工欲善其事, its prerequisite!

How to install pear and benchmark

Please refer to the PHP Performance Optimization series Phase I [PHP performance optimization preparation diagram pear installation]

Benchmark Tool Package Description

Direct download: http://pear.php.net/package/Benchmark/download
The benchmark tool pack has three files, namely timer.php, iterate.php, and profiler.php, and three tool classes feature the same, with a different focus, which is 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 obtained by the Microtime function.
The 2,benchmark_iterate class is used to debug the average execution time of a 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.

Detailed usage examples are available in three files.

How to get the execution time of a line or a piece of code

1, usually use the Microtime function to get the microsecond time before and after the code and then compare the difference of two values, as follows


But this method is very limited, not a wide range of applications, and every time you need to write a lot of code, suitable for simple debugging. Please see the PHP manual for details.

2, by using the Benchmark_timer class to get code to execute before and after the time difference, you can get n lines of code execution times, simple operation, only need to add a marker tag, see the following Benchmark_timer class usage instructions

How to use the Benchmark_timer class

The Benchmark_timer class only needs to add Benchmark_timer class initialization declarations and marker annotations in the debug file, and the end of the file prints the execution time at each label, as shown below

      require_once ' benchmark/timer.php '; $timer = new Benchmark_timer (); $timer->start (); $timer->setmarker (" Usleep (1), the $timer->setmarker ("marker"), Usleep (2), $timer->setmarker ("marker"), Usleep (3); $ marker Timer->stop (); $timer->display ();


There are two ways to print results:

One is the tabular output mode , $timer->display (); Such as



The other is manual var_dump or print_r printing , $timer->getprofiling (); Print_r function Prints as

    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 ' =&gt ; 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 label name, which contains two special labels, start and stop for starting and ending, followed by custom callouts marker marker 02
2,time indicates the current microsecond time
3,diff indicates 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 indicates execution to the current entire 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 more than once to get the average of its execution time, as in the following example:

      require_once "benchmark/iterate.php"; $bench = new benchmark_iterate;function Test ($i) {echo $i;} $bench->run ("test"), Var_dump ($bench->get ());


By calling the test function 100 times to get the average execution time, the results are as follows

      array1 = String ' 0.000486 ' (length=8) 2 = String ' 0.000466 ' (length=8) ........ ........... (omitted in middle), "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 the function executes, such as the average time to call 100 test functions on 0.000476
3,iterations indicates the number of function calls

How to use the Benchmark_profiler class


The Benchmark_profiler class is used for statistical functions such as the number of executions and execution times, for example:

      require_once ' benchmark/profiler.php '; $profiler = new Benchmark_profiler (TRUE); function myFunction () {global $ Profiler $profiler->entersection (' myFunction '); Do something $profiler->leavesection (' myFunction '); return;} Do Somethingmyfunction ();//do
    more


The results are as follows

Benchmark_profiler class is not used much in actual performance debugging, because there are better tools than this, such as Xdebuger, so can be directly ignored!

The Benchmark tool class is useful for analyzing program performance problems with progressive debugging in debugging, primarily by using the Benchmark_timer class to debug the point-in-time of each code snippet to improve the performance of the Code by acquiring execution time to optimize the program. No further discussion here, if there is any problem in the use of the process to welcome you to communicate!

If you find this progressive debugging is very tiring, if you want to grasp the overall performance of the program, this benchmark class debugging tool can not meet your needs, the next session 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 timestamp and number of microseconds
Description
Mixed Microtime ([bool Get_as_float])

Microtime () The current Unix timestamp and the number of microseconds. This function is available only under operating systems that support Gettimeofday () system calls.
If called without optional arguments, 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 msec is the microsecond portion. The two parts of a string are returned in seconds.

If the Get_as_float parameter is given and its value is equivalent to True,microtime (), a floating-point number is returned.

Note: The Get_as_float parameter is a new addition to PHP 5.0.0.

Extended Data
PHP Benchmark/timer Class
PHP Benchmark
Benchmark and Optimize PHP Script speed

http://www.bkjia.com/PHPjc/324694.html www.bkjia.com true http://www.bkjia.com/PHPjc/324694.html techarticle This is the second phase of the PHP performance Optimization series, how to use the Pear tool class benchmark to get the execution time of the code or function row by line. 工欲善其事, its prerequisite! How to install pear and Benchma ...

  • 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.