note 117769 deleted from function.call-user-func by cmb
| From: | cmb@php.net | Date: | Sun, 08 Dec 2019 11:16:14 +0000 |
| Subject: | note 117769 deleted from function.call-user-func by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213453@lists.php.net to get a copy of this message | ||
Note Submitter: Jordan LeDoux
----
I re-ran the test insta outlined 9 years ago to see how things had changed.
Variable functions took 0.014201879501343 seconds.
call_user_func took 0.044676065444946 seconds.
eval took 0.17955899238586 seconds.
As expected, eval() is slowes by a wide margin, and variable functions are fastest. However, 9 years
ago there weren't closures, and so I added them to the test for comparison.
closures took 0.012146949768066 seconds.
As you can see, closures ended up being the fastest, but all of these have experienced improvements.
I also increased the number of loops per test to 50,000 from 5,000. This was tested with PHP 5.5.9
on Ubuntu LTS.
The original test code was:
<?php
function fa () { return 1; }
function fb () { return 1; }
function fc () { return 1; }
$calla = 'fa';
$callb = 'fb';
$callc = 'fc';
$time = microtime( true );
for( $i = 50000; $i--; ) {
$x = 0;
$x += $calla();
$x += $callb();
$x += $callc();
if( $x != 3 ) die( 'Bad numbers' );
}
echo( "Variable functions took " . (microtime( true ) - $time) . " seconds.<br
/>" );
$time = microtime( true );
for( $i = 50000; $i--; ) {
$x = 0;
$x += call_user_func('fa', '');
$x += call_user_func('fb', '');
$x += call_user_func('fc', '');
if( $x != 3 ) die( 'Bad numbers' );
}
echo( "call_user_func took " . (microtime( true ) - $time) . " seconds.<br
/>" );
$time = microtime( true );
for( $i = 50000; $i--; ) {
$x = 0;
eval( '$x += ' . $calla . '();' );
eval( '$x += ' . $callb . '();' );
eval( '$x += ' . $callc . '();' );
if( $x != 3 ) die( 'Bad numbers' );
}
echo( "eval took " . (microtime( true ) - $time) . " seconds.<br />" );
?>
Here is the closures addition.
<?php
$funa = function() { return 1; };
$funb = function() { return 1; };
$func = function() { return 1; };
$time = microtime( true );
for ($i = 50000; $i--; ) {
$x = 0;
$x += $funa();
$x += $funb();
$x += $func();
if( $x != 3 ) die( 'Bad numbers' );
}
echo( "closures took " . (microtime( true ) - $time) . " seconds.<br />"
);
?>