note 105114 deleted from function.eval by danbrown

From: Date: Thu, 28 Jul 2011 19:07:48 +0000
Subject: note 105114 deleted from function.eval by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181969@lists.php.net to get a copy of this message
Note Submitter: jdomin at gmail dot com ---- This note is intended as an answer to http://www.php.net/manual/es/function.eval.php#82239. eval() IS NOT that slow. The problem with eval() is that the call itself has a certain overhead. I've rewritten the code in the previous example to add a new test, showing what happens when you execute the full loop in a single eval() call. If you test it, you will see that the cost of executing the loop through pre-parsed code is barely the same than the cost of executing it in a single eval() call. In fact, sometimes the single eval() approach gave me the best performance!! <?php //establish a blank integer $increment=0; //establish the code to be executed //one hundred million times $code="\$increment++;"; //remember the time this test started $started=time(); //execute $code on hundred million times for ($i=0;$i<10000000;$i++) { eval($code); } //find out how long it took, in //seconds $ended=time(); $spent=$ended-$started; //tell the user this print "Eval()ed code took $spent seconds to execute 100,000,000 times.\n"; //re-establish that same blank integer $increment=0; //remember the time this second test //started $started=time(); //execute the test again, with //single eval eval (' for ($i=0;$i<10000000;$i++) { $increment++; }'); //find out how long it took, in //seconds $ended=time(); $spent=$ended-$started; //tell the user this print "Single eval code took $spent seconds to execute 100,000,000 times.\n"; //re-establish that same blank integer $increment=0; //remember the time this third test //started $started=time(); //execute the test again, with //pre-parsed code for ($i=0;$i<10000000;$i++) { $increment++; } //find out how long it took, in //seconds $ended=time(); $spent=$ended-$started; //tell the user this print "Pre-parsed code took $spent seconds to execute 100,000,000 times.\n"; ?>

« previous php.notes (#181969) next »