Chapter 12. Zym_Timer

Chapter 12. Zym_Timer

Composed of Zym_Timer and Zym_Timer_Manager, This component was created for the purpose of timing runtime execution of PHP scripts. Mainly used by the Zym_Debug component to provide useful statistics for developers, the component allows standalone usage for cases where timing points in execution is not related to debugging.

Timers can keep track of multiple start/stop's which can be grouped to simulate nesting and accumulation.

12.1. Timing single runs

When you only need to measure the execution time between two points without the need to manage multiple timers, Zym_Timer can be used by itself.

Example 12.1. Code execution timing

<?php
 
$timer = new Zym_Timer();
 
// Start timer
$timer->start();
 
for ($x = 0; $x < 100000; ++$x) {
    // Long execution
}
 
// Stop timer
$runTime = $timer->stop(); // Returns time elapsed since start()

Retrieving the runtime from the timer object can be done several ways depending on your requirements. If you require the runtime of complete start and stops, the time can be retrieved from getRun(), but if you require the runtime with the current time elapsed if the timer was not stopped, then use getElapsed().

Example 12.2. Retrieving execution times

<?php
// Retrieve complete times, assume each start/stop is 1 second
$timer = new Zym_Timer();
$timer->start();
$time = $timer->stop(); // 1s
 
$timer->start();
$time = $timer->stop(); // 2s
 
// Time
$runTime = $timer->getRun(); // 2s
 
$timer->start();
// Get time elapsed
$runTime = $timer->getElapsed(); // 2.5s
$timer->stop();