PHP performance optimization tool article Benchmark class debugging execution time _ PHP Tutorial

Source: Internet
Author: User
Tags usleep
Debugging and execution time of the Benchmark class in the PHP performance optimization tool. 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! How to install PEAR and Benchma this is the second phase of the PHP performance optimization series, and how to use the PEAR tool class Benchmark to get the execution time of code or functions line by line.

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

How to install PEAR and Benchmark

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

Benchmark tool package description

Download: http://pear.php.net/package/Benchmark/download
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.

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

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.

How to use the Benchmark_Timer class

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.

How to use the Benchmark_Iterate class

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.

How to use the Benchmark_Profiler class


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.

Related Materials

Microtime

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

Extended Materials
PHP Benchmark/Timer Class
PHP Benchmark
Benchmark and Optimize PHP Script Speed

Bytes. To do what you want, you must first sharpen your skills! How to install PEAR and Benchma...

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.