Overview
This is a PHP class that calculates the time between specified points. It can be used to determine how long your page took to generate or how long certain loops take to run. This class is very flexible because it allows you to specify any number of “marker” points in the code and then compare any of them together.
There is nothing fancy about it. I ran across a post from someone looking for something like this and claimed it was hard to find… so I figured I’d release what we have in an effort to help anyone else out there.
download
class.benchmark.zip (610.00 bytes)
Current Version: 1.0
Source is hosted at Google Code
Features
- PHP5+ only
- Sets the first marker automatically
- Multiple markers
- Markers are stored so they can also be used as a totalizer
How To Use It
The use is really basic. Which is good, because let’s be honest.. it’s just a timer.
A basic use:
4 |
echo $bm->elapsed('start', 'end') . " Seconds"; |
Another use where we use other markers for something like sending an email
3 |
$bm->mark('email_start'); |
5 |
$bm->mark('email_stop'); |
7 |
echo $bm->elapsed('email_start', 'email_stop') . " Seconds"; |
Using it as a totalizer
01 |
$bm = new Benchmark(); |
04 |
foreach($emails as $email){ |
05 |
$bm->mark('email_start'); |
07 |
$bm->mark('email_stop'); |
09 |
$total_time += $bm->elapsed('email_start', 'email_stop'); |
12 |
echo $total_time . " Seconds"; |
Full Documentation
Initializing the Class
$bm = new Benchmark();
- When the class is initialized a marker is auto-generated called ’start’ through the __construct method.
Public Methods
- $this->mark(string $name)
-
- Sets a marker with the name that is passed in.
- $name – (string) Name of the marker you are setting
- $this->elapsed(string $point1, string $point2 [, int $decimals = 4])
- Gets the elapsed time between two marker points
- $point1 – (string) Name of the first marker you are checking
- $point2 – (string) Name of the second marker you are checking
- $decimal – (integer, optional) Number of decimal places you want displayed in the result