A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[PHP] Benchmarking Class

1k views · started by Chris ·
#1
[PHP] Benchmarking Class
<?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
$Benchmark = new Benchmark();

$Benchmark->Mark('start');
usleep(1000000);
$Benchmark->Mark('stop');

echo $Benchmark->Elapsed('start', 'stop');
?>
#2
Nice.

I did one similar for my framework:
<?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);
}

}

?>
Just noticed a huge bug when trying to get it to return data instead of echo'ing it, but whatever. Too late at night for me to fix it ATM.

Use it like so:
$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');


Outputs:
Benchmark[X++] took 0.38813710212708 seconds longer than Benchmark[++X]
#3
GAMEchief wrote:
Nice.

I did one similar for my framework:
<?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);
}

}

?>
Just noticed a huge bug when trying to get it to return data instead of echo'ing it, but whatever. Too late at night for me to fix it ATM.

Use it like so:
$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');


Outputs:
Benchmark[X++] took 0.38813710212708 seconds longer than Benchmark[++X]


Probably the same bug I came across.

Is it returning shit like;

1.E4-1.5

Or some weird shit like that?
#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).
#5
Fixed up my code, thanks to GAMEchief for posting his code.