Skip to content

Instantly share code, notes, and snippets.

@predominant
Created March 4, 2010 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save predominant/322223 to your computer and use it in GitHub Desktop.
Save predominant/322223 to your computer and use it in GitHub Desktop.
<?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