Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nette\Object performance tests

Nette\Object performance tests

I guess you already saw a short note Latency Numbers Every Programmer Should Know. I wanted to check how well Nette performs with its magic properties on Nette\Object.

You can see the testing code below together with raw output on my machine.

It show how much time in seconds it took to execute one million iterations of a particular action, thus it is also time in microseconds of one such call. You should compare it to null test, which execute empty iterations.

Results

Compared to a native getter of public property:

  • method call is cca 4x slower
  • magic getter is cca 15x slower
  • magic getter in form isSomething() is cca 25x slower

Summary

In a real app this does not cause any issue. I tested it on an our eshop where it added only cca 1ms to a page load, which was almost nothing to a total page load time.

<?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";
PHP: 5.3.11
null: 0.23424816131592
direct: 0.38207101821899
method: 0.66310882568359
magic: 6.6905748844147
magic2: 7.112911939621
---------------------------------
PHP: 5.3.12-dev
null: 0.16769599914551
direct: 0.25442695617676
method: 0.4458281993866
magic: 5.5972790718079
magic2: 7.195659160614
---------------------------------
PHP: 5.5.0-dev
null: 0.080867052078247
direct: 0.10454916954041
method: 0.20173192024231
magic: 3.9384489059448
magic2: 4.4427669048309
---------------------------------
PHP: 5.5.0-dev
null: 0.059589147567749
direct: 0.086474895477295
method: 0.17585802078247
magic: 2.877405166626
magic2: 3.1567361354828
---------------------------------
PHP: 5.4.0
null: 0.056331872940063
direct: 0.088710069656372
method: 0.17899608612061
magic: 2.8614220619202
magic2: 3.3825919628143
---------------------------------
@fprochazka

This comment has been minimized.

Copy link

@fprochazka fprochazka commented Aug 20, 2012

My mashine

PHP:    5.3.14
null:   0.38894200325012
direct: 0.52037191390991
method: 1.3765189647675
magic:  10.349886894226
magic2: 10.937663793564
@frogggias

This comment has been minimized.

Copy link

@frogggias frogggias commented Aug 20, 2012

PHP: 5.3.3
null: 0.21607804298401
direct: 0.34052395820618
method: 0.64685010910034
magic: 4.9356570243835
magic2: 5.409145116806

@hAdamm

This comment has been minimized.

Copy link

@hAdamm hAdamm commented Aug 21, 2012

PHP: 5.3.13
null: 0.18924808502197
direct: 0.29860186576843
method: 0.68159699440002
magic: 5.2286150455475
magic2: 5.9565918445587

@juzna

This comment has been minimized.

Copy link
Owner Author

@juzna juzna commented Aug 21, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment