Profiling your PHP scripts (performance benchmark)
This is going to be a short post. I’m going to show you my profiling class and explain it block by block, then I’m going to stop typing and click the “Publish” button on the bottom right hand corner, capisci? Good.
A little summary – “What is profiling & performance benchmarking anyways?”
This provides a way for you to determine how “long” it takes for PHP to parse a script from start to finish and then returns the time (in microseconds) so that you can judge how “well” you’re writing your scripts.
The profiler() class
<?php
// profiler class
class profiler {
public $start_time;function start() {
$t = microtime();
$time = explode (” “, $t);
$this->start_time = doubleval($time[0])+$time[1];
}function finish() {
$t = microtime();
$time = explode (” “, $t);
$end_time = doubleval($time[0])+$time[1];
echo substr(sprintf (”%01.4f”, $end_time-$this->start_time), 0, 6);
}
}// instantiate the class
$profile = new profiler;
$profile->start();
// code goes here to benchmark
$profile->finish();
?>
That’s the profiler() class. Now let’s break it down and see what it’s actually doing.
The start() function
function start() {
Store the current timestamp (in microseconds) into a variable for further reference (start the timer)
$t = microtime();
Take the microtime & the miliseconds (to the right) into an array (explode it)
$time = explode (” “, $t);
Fill the $start_time with the $t (microseconds) actual value (numbers only)
$this->start_time = doubleval($time[0])+$time[1];
}
The finish() function
function finish() {
Get the microtime again
$t = microtime();
You recognize this already
$time = explode (” “, $t);
You see it again
$end_time = doubleval($time[0])+$time[1];
Return the actual time subtracting this, from the actual $start_time (what we got from start())
sprintf returns a formatted string USING a format, in this case it’s %01.4f which says it’s a float (f)
and echo it.
echo substr(sprintf (”%01.4f”, $end_time-$this->start_time), 0, 5);
}
Now don’t get all smart on me and say “Yeah but how long does it take to run the profile class, because if you don’t know that then you..” shhh. It takes (I ran this) 0.0001 to 0.0002 seconds to run the profiler class.
The code for what I just said was simply this
$profile_profile = new profiler;
$profile_profile->start();
$profile = new profiler;
$profile->start();
$profile->finish();
$profile_profile->finish();
Which just tells you how long it takes to run it (you’re profiling your profiler)
So you can use this in all of your code to see how long it takes in microseconds (or full seconds) to run.
A couple of examples of benchmarked times (on an AMD K6-2 660MHz with 256MB PC100 RAM & 5400 RPM HD)
- It took me 0.00010 seconds to run this while loop to echo a value and increment by 1
$i = 0;
while ($i <= 200) {
echo $i . ‘<br />’;
$i++;
}
And that’s about it so if you’re any good at math you know that to run that loop 400 times it would take 0.00010.
This should help you judge your scripts quality & performance.
Hope this helps someone.
I eat styrofoam cups because I can’t,
bryan

