The Blueprint
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.
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:
$bm = new Benchmark();
//code here
$bm->mark('end');
echo $bm->elapsed('start', 'end') . " Seconds";
//note that 'start' was auto generated
Another use where we use other markers for something like sending an email
$bm = new Benchmark();
$bm->mark('email_start');
//email code here
$bm->mark('email_stop');
echo $bm->elapsed('email_start', 'email_stop') . " Seconds";
Using it as a totalizer
$bm = new Benchmark();
$total_time = 0;
foreach($emails as $email){
$bm->mark('email_start');
//email code here
$bm->mark('email_stop');
$total_time += $bm->elapsed('email_start', 'email_stop');
}
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
0 Comments
Search The Blog
Code & Projects
Categories
Archives
- October 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- February 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- April 2008
- March 2008
- February 2008
- July 2007
Download