note 83703 deleted from function.eval by danbrown

From: Date: Sun, 17 May 2009 19:52:39 +0000
Subject: note 83703 deleted from function.eval by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155284@lists.php.net to get a copy of this message
Note Submitter: Juan ---- 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.

« previous php.notes (#155284) next »