PHP performance optimization tool Benchmark class debugging execution time

Source: Internet
Author: User
Tags pear usleep
This is the second phase of the PHP performance optimization series. How to Use the PEAR tool Benchmark to get the execution time of code or functions line by line

This is the second phase of the PHP performance optimization series. How to Use the PEAR tool Benchmark to get the execution time of code or functions line by line

This is the second phase of the PHP performance optimization series. How to Use the PEAR tool Benchmark to get the execution time of code or functions line by line.

To do what you want, you must first sharpen your skills!



Please refer to the first part of the PHP performance optimization series [PHP performance optimization preparation illustration PEAR installation]



Download directly:
The Benchmark tool package contains three files: Timer. php, Iterate. php and Profiler. in php, the three tool classes have the same functions but have different focuses. They are used to debug the code to obtain the execution time of the program.

1. The Benchmark_Timer class principle is the same as obtaining the microsecond time through the microtime function and then comparing the difference between the first and second time values.
2. The Benchmark_Iterate class is used to debug the average execution time of a function.
3. The Benchmark_Profiler class is used to count the code and function execution time and the number of function calls.

Detailed usage examples are provided in the three files.



1. We usually use the microtime function to obtain the time difference between the two values before and after the code, as shown below:


However, this method is quite restrictive and cannot be used in a wide range. It requires a lot of code to be written every time and is suitable for simple debugging. For more information, see the PHP manual.

2. You can use the benchmark_Timer class to get the time difference before and after code execution. You can get the execution time of N lines of code at the same time. The operation is simple and you only need to add a marker mark, see the usage instructions of the Benchmark_Timer class below.



The Benchmark_Timer class only needs to add the Benchmark_Timer class initialization declaration and marker annotation to the debugging file. The execution time of each annotation is printed at the end of the file. The example is as follows:

    Require_once 'benchmark/Timer. php'; $ timer = new Benchmark_Timer (); $ timer-> start (); $ timer-> setMarker ("marker 01"); usleep (1 ); $ timer-> setMarker ("marker 02"); usleep (2); $ timer-> setMarker ("marker 03"); usleep (3 ); $ timer-> stop (); $ timer-> display ();


There are two ways to print the result:

One is table output., $ Timer-> display (); such



Manual var_dump or print_r Printing, $ Timer-> getProfiling ();, print_r function printing as shown in

    Array0 => array 'name' => string 'start' (length = 5) 'time' => string '123. 31334800 '(length = 19) 'diff' => string'-'(length = 1) 'Total' => string'-'(length = 1) 1 => array 'name' => string 'marker 01 '(length = 9) 'time' => string '2017. 31374400 '(length = 19) 'diff' => string '0. 000396 '(length = 8) 'Total' => string '0. 000396 '(length = 8) 2 => array 'name' => string 'marker 02' (length = 9) 'time' => string' 1265942405. 31423000 '(length = 19) 'diff' => string '0. 000486 '(length = 8) 'Total' => string '0. 000882 '(length = 8) 3 => array 'name' => string 'marker 03' (length = 9) 'time' => string '1265942405. 31519200 '(length = 19) 'diff' => string '0. 000962 '(length = 8) 'Total' => string '0. 001844 '(length = 8) 4 => array' Name'=> String 'stop' (length = 4 )' Time'=> String '2017. 100' (length = 19 )' Diff'=> String '0. 001046' (length = 8 )' Total'=> String '0. 002890' (length = 8)


Result description
1. name indicates the annotation name. The preceding two special annotations start and stop indicate the start and end, and the second is the custom annotation marker 01 marker 02.
2. time indicates the current microsecond time.
3,Diff indicates the execution time from the last tag to the current tagThis is the time difference we need to obtain. That's right. It's the value.
4. total indicates the entire execution time to the current time.



The Benchmark_Iterate class is used to debug the average time of function execution. Unlike the Benchmark_Timer class, the Benchmark_Timer class can call the same function multiple times to obtain the average execution time. The example is as follows:

    Require_once "Benchmark/Iterate. php "; $ response = new Benchmark_Iterate; function test ($ I) {echo $ I;} $ response-> run (100," test ", 10 ); var_dump ($ dumps-> get ());


Call the test function 100 times to obtain the average execution time. The result is as follows:

    Array1 => string '0. 000486 '(length = 8) 2 => string '0. 000466 '(length = 8 )............................. (omitted in the middle) 99 => string '0. 000479 '(length = 8) 100 => string '0. 000467 '(length = 8 )' Mean'=> String '0. 000476' (length = 8 )' Iterations'=> Int 100


Result description
1. Each number indicates the time of each call.
2. mean indicates the average time for function execution. The average time for calling the test function for 100 times is 0.000476.
3. iterations indicates the number of function calls.


The Benchmark_Profiler class is used to count the number of function executions and execution time. The example is as follows:

    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 result is as follows:

Benchmark_Profiler class is not used in actual performance debugging, because there are better tools such as xDebuger, so you can ignore them directly!

The Benchmark tool class is very useful for analyzing program performance problems in line-by-line debugging. It mainly uses the Benchmark_Timer class to debug the time points of each code segment, to optimize the program and improve the Code performance by obtaining the execution time. I will not discuss it in depth here. If you have any questions during use, please join us!

If you find this kind of line-by-line debugging very tired and hard, if you want to grasp the overall performance of the program, this Benchmark debugging tool cannot meet your needs, next, we will discuss how to install and use the PHP performance debugging tool xDebuger.



(PHP 3, PHP 4, PHP 5)

Microtime -- returns the current Unix Timestamp and microseconds.
Description
Mixed microtime ([bool get_as_float])

Microtime () Current Unix Timestamp and number of microseconds. This function is only available in the operating system that supports the gettimeofday () System Call.
If no optional parameter is required, this function returns a string in the format of "msec sec", where sec is the current number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT, msec is in microseconds. The two parts of the string are returned in seconds.

If the get_as_float parameter is given and its value is equivalent to TRUE, microtime () returns a floating point number.

Note: The get_as_float parameter is newly added to PHP 5.0.0.

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.