Use the pear: Benchmarking timer to implement PHP program timing

Source: Internet
Author: User

// Create an object
$ Timer = new benchmark_timer ();

// Start timing
$ Timer-> Start ();

// Test a script
For ($ I = 0; I I <1000; $ I ++)
{
// We do nothing here
}

// Set the tag
$ Timer-> setmarker ("mark1 ");

// When two points are output
Echo $ timer-> timeelapsed ("START", "mark1 ");
?>
Then, we can get the result 0.000543, which is roughly the time consumed from the start point to the mark1 point.
Class Method
Method: benchmark_timer ([mixed $ auto = false])
Description: constructor, which starts the timer record.
Parameter: Boolean $ auto. The default value is false. If it is set to true, the timer result is automatically printed.
Time index ex time %
Start 1099020859.80505200-0.00%
Stop 1099020859.80595000 0.000898 100.00%
Total-0.000898 100.00%

The START/End Time indexes are listed separately, and the running time between the last tag as a percentage of the total time consumed (so the stop point is always 100% ), and total time consumption.
Method: Display ()
Description: prints the information returned by the getoutput method.
See the following table:
Time index ex time %
Start 1099021292.32145600-0.00%
Mark1 1099021292.32202000 0.000564 46.38%
Mark2 1099021292.32209700 0.000077 6.33%
Stop 1099021292.32267200 0.000575 47.29%
Total-0.001216 100.00%

Lists the Time Indexes of each tag, the running time between the last tag, the percentage of running time between the last tag and the total time consumption.
Before calling this method, call stop () to terminate the timer (and the program will not be terminated). See example 2. Method: getoutput ()
Description: returns the formatted timer information. This method places the timer information in a table (such as the table above) for the display () method.
Method: getprofiling ()
Description: returns the timer information, as shown in the following figure.
Array
(
[0] => Array
(
[Name] => Start
[Time] = & gt; 1099021787.69669100
[Diff] =>-
[Total] = & gt; 1099021787.696691
)

[1] => Array
(
[Name] => mark1
[Time] = & gt; 1099021787.69675900
[Diff] = & gt; 0.000068
[Total] = & gt; 1099021787.696759
)

[2] => Array
(
[Name] => mark2
[Time] = & gt; 1099021787.69683500
[Diff] = & gt; 0.000076
[Total] = & gt; 1099021787.696835
)

[3] => Array
(
[Name] => stop
[Time] = & gt; 1099021787.69694500
[Diff] = & gt; 0.000110
[Total] = & gt; 1099021787.696945
)

)
. 0, 1, 2... the location where the tag is located. assume that X is the position of a tag, then $ profiling [x] ['name'] = mark the name of x $ profiling [x] ['time'] = time index marking x $ profiling [x] ['diff '] = time difference between running from the X-1 tag to the X tag $ profiling [x] ['Total'] = all running times before marking X.
Method: setmarker (string $ name)
Description: sets the tag.
Parameter: String $ name-name of the tag to be set, case sensitive.

Method: Start ()
Description: sets the Start mark, which also indicates the start of timing.
Method: Stop ()
Description: sets the stop flag, which also indicates the timing termination.

Method: Double timeelapsed ([String $ start = 'start'], [String $ end = 'stop'])
Description: the time consumed between two tags.
Parameters:
String $ start-start tag. The default value is "start"
String $ end-End mark. The default value is "stop"
Contains 6 decimal places.
Application
1. record the time difference between two points
// Create an object
$ Timer = new benchmark_timer ();

// Set the tag ("mark1 ")
$ Timer-> setmarker ("mark1 ");

// Perform a test
For ($ I = 0; I I <100; $ I ++)
{
// We do nothing here
}

$ Timer-> setmarker ("mark2 ");

// When two points are output
Echo $ timer-> timeelapsed ("mark1", "mark2 ");
2. List timer information.
// Create an object
$ Timer = new benchmark_timer ();

// Start timing
$ Timer-> Start ();

$ Timer-> setmarker ("mark1 ");

// Perform a test
For ($ I = 0; I I <100; $ I ++)
{
// We do nothing here
}

$ Timer-> setmarker ("mark2 ");

// When two points are output
Echo $ timer-> timeelapsed ("mark1", "mark2 ");

// Abort timing
$ Timer-> stop ();
$ Timer-> display ();
Summary
Obviously, if you want to, you can set countless tags in the program. If one of your programs runs slowly, you may use it to test which program takes the longest time, quickly locate the problem.
 

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.