- microtime: documentation ( source)
- mt_rand: documentation ( source)
- number_format: documentation ( source)
- printf: documentation ( source)
<?php
function foo() {
$bar = mt_rand();
}
$iterations = 1000000;
$a = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$bar = mt_rand();
}
$b = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
foo();
}
$c = microtime(true);
$ab = $b - $a;
$bc = $c - $b;
$abcPercent = (($bc / $ab) - 1) * 100;
printf('Direct: %s seconds' . PHP_EOL, number_format($ab, 4));
printf('Function: %s seconds (%s%% more!)', number_format($bc, 4), number_format($abcPercent, 2));