|
<?php |
|
/** |
|
* Comparing speed of: |
|
* - native property access |
|
* - getter method |
|
* - magic property access on nette object ($x->name --> $x->getName()) |
|
* - magic property access with isBar() method |
|
*/ |
|
|
|
define('ITERATIONS', 1e6); |
|
require_once '/home/juzna/projects/nette/Nette/loader.php'; |
|
echo "PHP:\t", PHP_VERSION, "\n"; |
|
|
|
|
|
/** |
|
* Class under test |
|
*/ |
|
class Foo extends \Nette\Object |
|
{ |
|
public $name1; |
|
private $name2; |
|
|
|
public function __construct($a, $b) |
|
{ |
|
$this->name1 = $a; |
|
$this->name2 = $b; |
|
} |
|
|
|
public function getName1() { return $this->name1; } |
|
public function getName2() { return $this->name2; } |
|
public function isName3() { return $this->name2; } |
|
|
|
} |
|
|
|
|
|
|
|
/***************** helpers *****************j*d*/ |
|
|
|
function slowdown($t1, $t0) |
|
{ |
|
return round(($t1 - $t0) / ITERATIONS * 1e6, 3) . 'us'; |
|
} |
|
|
|
/***************** eof: helpers *****************j*d*/ |
|
|
|
|
|
|
|
// make sure that it all works as expected |
|
$foo = new Foo('a', 'b'); |
|
//assert(false); |
|
assert($foo->name1 === 'a'); |
|
assert($foo->getName1() === 'a'); |
|
assert($foo->name2 === 'b'); |
|
assert($foo->name3 === 'b'); |
|
|
|
|
|
|
|
/***************** test 0 - empty assignment in cycle *****************j*d*/ |
|
$t = microtime(true); |
|
for($i = 0; $i < ITERATIONS; $i++) { |
|
$x = 0; |
|
} |
|
echo "null:\t", $result0 = microtime(true) - $t, "\n"; |
|
|
|
|
|
|
|
/***************** test 1 - direct property access *****************j*d*/ |
|
$t = microtime(true); |
|
for($i = 0; $i < ITERATIONS; $i++) { |
|
$x = $foo->name1; |
|
} |
|
echo "direct:\t", $result1 = microtime(true) - $t, "\n"; |
|
|
|
|
|
|
|
/***************** test 2 - getter method call *****************j*d*/ |
|
$t = microtime(true); |
|
for($i = 0; $i < ITERATIONS; $i++) { |
|
$x = $foo->getName2(); |
|
} |
|
echo "method:\t", $result2 = microtime(true) - $t, "\n"; |
|
|
|
|
|
|
|
/***************** test 3 - magic property *****************j*d*/ |
|
$t = microtime(true); |
|
for($i = 0; $i < ITERATIONS; $i++) { |
|
$x = $foo->name2; |
|
} |
|
echo "magic:\t", $result3 = microtime(true) - $t, "\n"; |
|
|
|
|
|
|
|
/***************** test 4 - magic property (is) *****************j*d*/ |
|
$t = microtime(true); |
|
for($i = 0; $i < ITERATIONS; $i++) { |
|
$x = $foo->name3; |
|
} |
|
echo "magic2:\t", $result4 = microtime(true) - $t, "\n"; |
|
|
Be careful if you've got some debugging tools enabled. xdebug can make things much slower because it does some extra work on every instruction.
You can disable most stuff just by skipping your php.ini:
php -c /dev/null property-read.php