note 83703 added to function.eval

From: Date: Sun, 08 Jun 2008 13:23:38 +0000
Subject: note 83703 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-140240@lists.php.net to get a copy of this message
in response to: Luke at chaoticlogic dot net The Problem why your code using eval runs that slow is, that you are executing 10,000,000 times the eval construct instead executing it just once and including the loop inside the eval'd code: <?php // Like this it will run the eval function 10,000,000 times and // thus the eval'd code 10,000,000 times too: $increment=0; $code="\$increment++;"; for ($i=0;$i<10000000;$i++) { eval($code); } // Like this, the eval will only be executed once, but the code // inside the loop inside the eval will be executed the // 10,000,000 times as desired. $increment=0; $code='for ($i=0;$i<10000000;$i++) { $increment++; }'; eval($code); // Running the code with the loop inside the eval'd code made // no measurable difference for me, compared to running it // directly like this: $increment=0; for ($i=0;$i<10000000;$i++) { $increment++; } ?> The reason for your code being so much slower is simple: Your Code does: 10,000,000 x eval() 10,000,000 x increment My code does: 1 x eval() 10,000,000 x increment As the eval construct is far more complex (I guess the parser has to be started every time) than the increment assgnment, it is obvious, that your code will need much more time to run. ---- Server IP: 192.35.244.50 Probable Submitter: 212.108.37.237 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/83703 Del: integrated -- https://master.php.net/note/delete/83703/integrated Del: useless -- https://master.php.net/note/delete/83703/useless Del: bad code -- https://master.php.net/note/delete/83703/bad+code Del: spam -- https://master.php.net/note/delete/83703/spam Del: non-english -- https://master.php.net/note/delete/83703/non-english Del: in docs -- https://master.php.net/note/delete/83703/in+docs Del: other reasons-- https://master.php.net/note/delete/83703 Reject -- https://master.php.net/note/reject/83703 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140240) next »