<?php
function report($name, $time, $base) {
printf("%-12s: %.8fms", $name, $time);
printf(", %.1f%%", (($time - $base)/$base)*100);
echo PHP_EOL;
}
$iterations = 100000;
$a = str_repeat('a', 2<<8);
$times = array(
"loop" => 0,
"direct" => 0,
"static" => 0,
"object" => 0
);
class Os {
public static function strcmp($a) {
strcmp($a, $a);
}
}
class Om {
public function strcmp($a) {
strcmp($a, $a);
}
}
$o = new Om();
$start = microtime(true);
for ($x=0; $x<$iterations; ++$x) {}
$end = microtime(true) - $start;
$times["loop"]+= $end;
$start = microtime(true);
for ($x=0; $x<$iterations; ++$x) {
strcmp($a, $a);
}
$end = microtime(true) - $start;
$times["direct"]+= $end;
$start = microtime(true);
for ($x=0; $x<$iterations; ++$x) {
Os::strcmp($a);
}
$end = microtime(true) - $start;
$times["static"]+= $end;
$start = microtime(true);
for ($x=0; $x<$iterations; ++$x) {
$o->strcmp($a);
}
$end = microtime(true) - $start;
$times["object"]+= $end;
foreach ($times as $name => $time) {
report($name, ($time/$iterations)*1000, ($times['loop']/$iterations)*1000);
}
preferences:
38.6 ms | 407 KiB | 5 Q