note 96593 added to function.eval

From: Date: Sat, 06 Mar 2010 19:59:14 +0000
Subject: note 96593 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-166745@lists.php.net to get a copy of this message
It occurred to me that both of the examples I saw here, comparing eval() and include() were fatally flawed in their analysis. They have confused the process of 'importing' the code and evaluating it with actually running that code. In order to test what's actually going on I revised the code and ran tests on my atom server using the linux PHP install on Xubuntu. These are the results: (Conclusion follows after source code.) Test 1 Fired Eval()ed code took 2.68046 seconds to execute 3,000,000 times. The eval() process took 2.0E-5 seconds to processs. Test 2 Fired Include()ed code took 2.59646 seconds to execute 3,000,000 times. The include() process took 0.00042 seconds to processs. Test 3 Fired Native code took 2.47357 seconds to execute 3,000,000 times. My methodology was to define the same function by 3 different names in 3 different ways (eval, include and native code). Once the code was 'imported' I then executed it 3,000,000 times each. In addition I also used microtime() to measure the time it took to evaluate and include. My source is as follows: test.php <?php //Establish a blank integer and the test states. $Increment=0; $Test1Fired = false; $Test2Fired = false; $Test3Fired = false; //Define tests $Code="function Test1() { global \$Increment,\$Test1Fired; if (\$Test1Fired === false) { print 'Test 1 Fired\n'; \$Test1Fired = true; } \$Increment++; }"; //We time the include Process (NOT code execution from include) $IncludeTimer['Start']=microtime(true); include 'test_include.php'; $IncludeTimer['End']=microtime(true); $IncludeTimer['Spent']=$IncludeTimer['End']-$IncludeTimer['Start']; function Test3() { global $Increment,$Test3Fired; if ($Test3Fired === false) { print "Test 3 Fired\n"; $Test3Fired = true; } $Increment++; } //Running Test 1 $Started=microtime(true); //We evaluate the code once, as we are examining the efficiency of code brought in with the eval command, NOT the speed at which the eval command works. //In order to test how quickly the eval does work though, let's do another test: $EvalTimer['Start']=microtime(true); eval($Code); $EvalTimer['End']=microtime(true); $EvalTimer['Spent']=$EvalTimer['End']-$EvalTimer['Start']; for ($i=0;$i<3000000;$i++) { Test1(); } $Ended=microtime(true); $Spent=$Ended-$Started; //Tell the user this print "Eval()ed code took ".round($Spent,5)." seconds to execute 3,000,000 times.\n"; print "The eval() process took ".round($EvalTimer['Spent'],5)." seconds to processs.\n\n"; //Re-establish that same blank integer $Increment=0; $Started=microtime(true); //This time we use the code brought in from the included file. for ($i=0;$i<3000000;$i++) { Test2(); } $Ended=microtime(true); $Spent=$Ended-$Started; //Tell the user this print "Include()ed code took ".round($Spent,5)." seconds to execute 3,000,000 times.\n"; print "The include() process took ".round($IncludeTimer['Spent'],5)." seconds to processs.\n\n"; //Now we do a final Test3, which is code that was already in the file anyway. //Re-establish that same blank integer $Increment=0; $Started=microtime(true); //This time we use the code already in the file. for ($i=0;$i<3000000;$i++) { Test3(); } $Ended=microtime(true); $Spent=$Ended-$Started; //Tell the user this print "Native code took ".round($Spent,5)." seconds to execute 3,000,000 times.\n\n"; ?> test_include.php <? function Test2() { global $Increment,$Test2Fired; if ($Test2Fired === false) { print "Test 2 Fired\n"; $Test2Fired = true; } $Increment++; } ?> In conclusion, the difference really is minimal, but native is fastest, include is 96% of the speed of native and eval about 92% of the speed of native. Nothing that makes me loose any sleep! ---- Server IP: 69.147.83.197 Probable Submitter: 80.229.196.130 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/96593 Del: integrated -- https://master.php.net/note/delete/96593/integrated Del: useless -- https://master.php.net/note/delete/96593/useless Del: bad code -- https://master.php.net/note/delete/96593/bad+code Del: spam -- https://master.php.net/note/delete/96593/spam Del: non-english -- https://master.php.net/note/delete/96593/non-english Del: in docs -- https://master.php.net/note/delete/96593/in+docs Del: other reasons-- https://master.php.net/note/delete/96593 Reject -- https://master.php.net/note/reject/96593 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166745) next »