
Originally Posted by
bokehman
The biggest thing you are testing in that test is actually memory usage.
Here is a far more sensible test for what you're trying to test:
PHP Code:
<?php
class Tester {
public $var = 0;
public function staticMethod() {
static $var = 0;
return ++$var;
}
public function dynamicMethod() {
return ++$this->var;
}
}
$n = 100;
$tester = new Tester();
$time = microtime(true);
for ($x=0; $x<$n; $x++) {
$tester->dynamicMethod();
}
echo "DYNAMIC: " . number_format($dynamic = (microtime(true) - $time), 5) . "\n";
$time = microtime(true);
for ($x=0; $x<$n; $x++) {
$tester->staticMethod();
}
echo "STATIC: " . number_format($static = (microtime(true) - $time), 5) . "\n";
echo 'RATIO: ' . number_format($dynamic / $static, 5) . "\n\n";
When $n = 100:
Code:
$ php ./test-static.php
DYNAMIC: 0.00152
STATIC: 0.00139
RATIO: 1.08740
When $n = 1000:
Code:
$ php ./test-static.php
DYNAMIC: 0.01447
STATIC: 0.01882
RATIO: 0.76904
When $n = 5000:
Code:
$ php ./test-static.php
DYNAMIC: 0.07730
STATIC: 0.12661
RATIO: 0.61050
Bookmarks