Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class A {
private static $__value = 'Foo';
public function getValue() {
return self::$__value;
}
}
class B {
private static $__value = 'Foo';
public function getValue() {
return static::$__value;
}
}
$iterations = 5000000;
$results = array();
$a = new A();
for ($loop = 0; $loop < 5; $loop++) {
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$a->getValue();
}
$elapsed = microtime(true) - $start;
$results[] = $elapsed;
}
unset($a);
$average = array_sum($results) / count($results);
echo "self:: => $average\n";
$results = array();
$b = new B();
for ($loop = 0; $loop < 5; $loop++) {
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$b->getValue();
}
$elapsed = microtime(true) - $start;
$results[] = $elapsed;
}
unset($b);
$average = array_sum($results) / count($results);
echo "static:: => $average\n";
// Results (PHP 5.3.0 cli)
// self:: => 2.2726522445679
// static:: => 2.2912198543549
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment