note 61553 added to function.call-user-func

From: Date: Mon, 06 Feb 2006 20:15:29 +0000
Subject: note 61553 added to function.call-user-func
Groups: php.notes 
Request: Send a blank email to php-notes+get-103137@lists.php.net to get a copy of this message
I benchmarked the comparison in speed between variable functions, call_user_func, and eval. My results are below: Variable functions took 0.125958204269 seconds. call_user_func took 0.485446929932 seconds. eval took 2.78526711464 seconds. This was run on a Compaq Proliant server, 180MHz Pentium Pro 256MB RAM. Code is as follows: <?php function fa () { return 1; } function fb () { return 1; } function fc () { return 1; } $calla = 'fa'; $callb = 'fb'; $callc = 'fc'; $time = microtime( true ); for( $i = 5000; $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 = 5000; $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 = 5000; $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 />" ); ?> ---- Server IP: 216.194.113.175 Probable Submitter: 69.150.149.197 ---- Manual Page -- http://www.php.net/manual/en/function.call-user-func.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61553 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61553&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61553&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61553&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61553&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61553&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61553&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61553&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61553&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103137) next »