- microtime: documentation ( source)
- printf: documentation ( source)
<?php
function foo() {
return 1234;
}
class Foo {
public static function asdf() {
return 1234;
}
}
$iterations = 1000000;
$process = foo();
$process = Foo::asdf();
// Old
$a = microtime( true );
for ( $i = 0; $i < $iterations; $i++ ) {
$process = foo();
}
$b = microtime( true );
$delta1 = $b - $a;
// New
$x = microtime( true );
for ( $j = 0; $j < $iterations; $j++ ) {
$process = Foo::asdf();
}
$y = microtime( true );
$delta2 = $y - $x;
$percentage = $delta2 / $delta1 * 100;
if ( $delta2 < $delta1 ) {
printf( 'static method is %.2f%% faster', $percentage );
} else {
printf( 'function call is %.2f%% faster', $percentage );
}