Benchmark_Timer

Benchmark_Timer – About Benchmark_Timer

About Benchmark_Timer

As mentioned before, this class simply records the time taken for a particular snippet of code to execute. In the automatic mode, this means the code remaining after the class's instantiation. In manual mode, the snippet of code to be timed is specified between calls to the start() and stop() methods of the class.

Additionally, you may specify markers between the snippet of code to be timed. The timing information is recorded wherever markers are defined.

An example will make this clear.

Benchmark_Timer in automatic mode

<?php
require_once "Benchmark/Timer.php";

$timer = new Benchmark_Timer(TRUE);
// Timer is in automatic mode, so timing starts here.

$j = array();
for (
$i 0$i 100$i++) {
    
$j[] = $i;
    if (
$i == 49)
        
$timer->setMarker('Midway');
}

// Timer automatically stops and results are displayed.
?>

The example above will generate an output as shown below when run with the PHP CLI SAPI. Running it with the Apache SAPI will produce the same results, but formatted as HTML.

Results of a test run of Benchmark_Timer

----------------------------------------------------
marker  time index            ex time         perct   
----------------------------------------------------
Start   1182558324.48638900   -                0.00%
----------------------------------------------------
Midway  1182558324.48655800   0.000169        62.13%
----------------------------------------------------
Stop    1182558324.48666100   0.000103        37.87%
----------------------------------------------------
total   -                     0.000272       100.00%
----------------------------------------------------

In most cases however, you would like to do something with that information. When you run the timer in manual mode, you can obtain the results as an associative array using the getProfiling() method. To display the information (as shown above) use the display() method. More information on the methods that Benchmark_Timer implements is available in the API Documentation.