PHP Code:
<?php
class Benchmark{
private $ElapsedTime = Array();
function __destruct(){
unset($ElapsedTime);
}
public function Mark($Handle){
$this->ElapsedTime[$Handle] = (float) microtime(true);
}
public function Elapsed($Handle_Start, $Handle_Stop, $Decimals=false){
$ElapsedTime = 0;
if(isset($this->ElapsedTime[$Handle_Start]) && $this->ElapsedTime[$Handle_Start] > 0 && isset($this->ElapsedTime[$Handle_Stop]) && $this->ElapsedTime[$Handle_Stop] > 0){
if($Decimals !== false){
$ElapsedTime = round(($this->ElapsedTime[$Handle_Stop] - $this->ElapsedTime[$Handle_Start]), $Decimals);
}else{
$ElapsedTime = abs($this->ElapsedTime[$Handle_Stop] - $this->ElapsedTime[$Handle_Start]);
}
}
return $ElapsedTime;
}
}
?>
Due to the fact many people may not understand it, it is rather simple here is an example;
PHP Code:
<?php
$Benchmark = new Benchmark();
$Benchmark->Mark('start');
usleep(1000000);
$Benchmark->Mark('stop');
echo $Benchmark->Elapsed('start', 'stop');
?>
Results 1 to 5 of 5
Thread: [PHP] Benchmarking Class
- 20 Nov. 2009 02:14am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 500.00
[PHP] Benchmarking Class
- 20 Nov. 2009 04:29am #2
Nice.
I did one similar for my framework:
PHP Code:<?php
class Benchmark
{
var $times = Array(
'start' => Array(),
'finish' => Array()
);
var $type = 'txt';
function compare($key1 = 0, $key2 = 1, $return = false)
{
$difference = $this->difference($key2) - $this->difference($key1);
if ($return)
return $difference;
else
{
echo 'Benchmark[', ($difference > 0 ? $key2 : $key1), '] took ', abs($difference), ' seconds longer than Benchmark[', ($difference > 0 ? $key1 : $key2), ']', ($this->type == 'html' ? '<br />' : "\n");
}
}
function difference($key)
{
return ($this->times['finish'][$key] - $this->times['start'][$key]);
}
function finish($key = 0)
{
$this->times['finish'][$key] = (float) microtime(true);
}
function result($key = 0, $return = false)
{
if ($return)
return $this->difference($key);
else
echo 'Benchmark[' . $key . '] took ' . $this->difference($key) . ' seconds.' . ($this->type == 'html' ? '<br />' : "\n");
}
function results($key = false, $return = false)
{
if ($key)
{
if (is_array($key))
{
$keys = count($key);
$k = 0;
while ($k < $keys)
{
$this->result($key[$k]);
++$k;
}
}
else
$this->result($key);
}
else
{
foreach ($this->times['start'] as $key => $value)
{
if (array_key_exists($key, $this->times['finish']))
$this->result($key);
}
}
}
function start($key = 0)
{
$this->times['start'][$key] = (float) microtime(true);
}
}
?>
Use it like so:PHP Code:$bm = new Benchmark();
$bm->type = 'html'; // output line breaks as <br /> instead of \n
$bm->start('X++');
for ($x = $y = 0; $x < 3000000; $x++) { $y++; }
$bm->finish('X++');
$bm->start('++X');
for ($x = $y = 0; $x < 3000000; ++$x) { ++$y; }
$bm->finish('++X');
$bm->compare('X++', '++X');
Benchmark[X++] took 0.38813710212708 seconds longer than Benchmark[++X]
- 20 Nov. 2009 04:37am #3
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
- 20 Nov. 2009 04:46am #4
No, no. I mean, I coded it wrong and forgot to actually return the value. I never tested it with returning, as I originally planned and used it to echo, so I didn't focus much on returning, and thus I missed a bit of the code.
The xey is scientific notation, which means X*10^Y, used to display ridiculously small or large numbers (small in this case). I just increased my loops to get rid of that (as you can see it goes up to 3 million).
- 24 Nov. 2009 02:47am #5
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00