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 ); }