write some code. people often are saying this is faster then that, this is slower that that. well personal opinion is it depends alot on you hardware/os. write some code. what is more important then someone finding code faster on their machine, is how fast is it on yours. case is slow on my machine.
<?php
class debug
{
function debug()
{
$this->start = $this->debug_time();
}
function reset()
{
$all_vars = get_object_vars($this);
foreach($all_vars as $pos => $val)
if ($pos != 'error')
unset($this->$pos);
}
function debug_time()
{
$time = microtime();
$time = str_replace('.', '', $time);
$time = explode(' ', $time);
$time = $time[1] . $time[0];
return($time);
}
function time($text = '')
{
$this->break = $this->debug_time();
$time = ($this->break - $this->start) / 1000000000;
error_log(number_format($time, 4) ." : $text \n", 3, '/var/log/php_err.log');
$this->start = $this->break;
}
}
$debug = new debug;
$debug->time('a');
for($c = 0; $c < 100000; $c++)
if ( $c % 2 )
break;
else
break;
$debug->time('b');
for($c = 0; $c < 100000; $c++)
switch ( $c % 2 )
{
case 0:
break;
case 1:
break;
}
$debug->time('c');
?>
0.0001 : a
0.0002 : b
3.0034 : c
P+133 96mb ram openbsd 2.9
0.0028 : a
0.0047 : b
0.9631 : c
PIII-550 512mb ram Linux 2.2.18
if/else is faster on my P133 but case is aster on my PIII-550 its to hardware dependant.
use this code to check benchmarks.
tail -f /var/log/php_err.log
or wherever your php errors are sent to.
Chris Lee
lee@mediawaveonline.com