Hi all.
During the RFC doscussion today, the question popped up how the performance of creating closures compares to creating objects. This is particularly relevant for closures/objects created by bootstrap code which is always executed, e.g. when registering with a CI framework.
Attached is a benchmark I quickly hacked up. It indicates that creating objects is about 40% slower on my setup (PHP 5.4.9). I'd be curious to know how it compares on HHVM.
In absolute numbers though, creating an object seems to take about one *micro*second. That seems fast enough that we don't really have to care, I think.
Anyone want to try?
Cheers, daniel
Apperently the attached file got stripped when posting to the list. Here's a link:
http://brightbyte.de/repos/codebin/ClosureBenchmark.php?view=1
Here is the code inlined:
<?php
function timeClosures( $n ) { $start = microtime( true );
for ( $i = 0; $i < $n; $i++ ) { $closure = function( $x ) use ( $i ) { return $i*$x; }; }
$sec = microtime( true ) - $start; print " It took $sec seconds to create $n closures.\n";
return $sec; }
class ClosureBenchmarkTestClass { private $x;
public function __construct( $x ) { $this->x = $x; }
public function foo( $y ) { return $this->x * $y; } }
function timeObjects( $n ) { $start = microtime( true );
for ( $i = 0; $i < $n; $i++ ) { $obj = new ClosureBenchmarkTestClass( $i ); }
$sec = microtime( true ) - $start; print " It took $sec seconds to create $n objects.\n";
return $sec; }
$m = 10; $n = 1000000;
for ( $i = 0; $i < $m; $i++ ) { $ctime = timeClosures( $n ); $otime = timeObjects( $n );
$dtime = $ctime - $otime; $rtime = ( $ctime / $otime ); $fasterOrSlower = $dtime > 0 ? 'faster' : 'slower'; print sprintf( "Creating %d objects was %f seconds %s (%d%%).\n", $n, abs( $dtime ), $fasterOrSlower, abs( $rtime ) * 100 ); }
On Thu, Sep 11, 2014 at 1:02 AM, Daniel Kinzler daniel@brightbyte.de wrote:
Hi all.
During the RFC doscussion today, the question popped up how the performance of creating closures compares to creating objects. This is particularly relevant for closures/objects created by bootstrap code which is always executed, e.g. when registering with a CI framework.
Attached is a benchmark I quickly hacked up. It indicates that creating objects is about 40% slower on my setup (PHP 5.4.9). I'd be curious to know how it compares on HHVM.
In absolute numbers though, creating an object seems to take about one *micro*second. That seems fast enough that we don't really have to care, I think.
Anyone want to try?
3v4l can do HHVM as well: http://3v4l.org/barGj
wikitech-l@lists.wikimedia.org