note 117769 added to function.call-user-func

From: Date: Wed, 05 Aug 2015 21:24:16 +0000
Subject: note 117769 added to function.call-user-func
Groups: php.notes 
Request: Send a blank email to php-notes+get-203271@lists.php.net to get a copy of this message
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 />" ); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 12.9.242.254 ---- Manual Page -- http://php.net/manual/en/function.call-user-func.php Edit -- https://master.php.net/note/edit/117769 Del: integrated -- https://master.php.net/note/delete/117769/integrated Del: useless -- https://master.php.net/note/delete/117769/useless Del: bad code -- https://master.php.net/note/delete/117769/bad+code Del: spam -- https://master.php.net/note/delete/117769/spam Del: non-english -- https://master.php.net/note/delete/117769/non-english Del: in docs -- https://master.php.net/note/delete/117769/in+docs Del: other reasons-- https://master.php.net/note/delete/117769 Reject -- https://master.php.net/note/reject/117769 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203271) next »