note 102528 added to function.eval

[email protected] Sat, 19 Feb 2011 07:04:45 -0800
Newsgroups php.notes
Message-ID <[email protected]>
using the test given by Luke at chaoticlogic dot net, I modified it a little and added a test in which the ENTIRE loop was eval()'ed (not just the loop contents).

<?php
//establish a blank integer
$increment=0;

//establish the code to be executed
$code="\$increment++;";

//remember the time this test started
$started=time();

//execute $code one 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 LOOP CONTENTS took $spent seconds to execute 10,000,000 times.\n";

//re-establish a blank integer
$increment=0;

//establish the new code to be executed
$code="for(\$i=0;\$i<10000000;\$i++)\$increment++;";

//remember the time this test started
$started=time();

//execute $code
eval($code);

//find out how long it took, in
//seconds
$ended=time();
$spent=$ended-$started;

//tell the user this
print "Eval()ed COMPLETE LOOP took $spent seconds to execute 10,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
//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 10,000,000 times.\n";
?>

On my system (PHP 5.2.9), this gives the following output:

Eval()ed LOOP CONTENTS took 38 seconds to execute 10,000,000 times.
Eval()ed COMPLETE LOOP took 2 seconds to execute 10,000,000 times.
Pre-parsed code took 1 seconds to execute 10,000,000 times.

So, the overhead of eval() appears to largely be in the initial call itself, the actual speed of bulk code inside eval() is many orders faster than repeated, smaller eval() calls.
----
Server IP: 192.41.42.26
Probable Submitter: 98.16.181.164
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit        -- https://master.php.net/note/edit/102528
Del: integrated  -- https://master.php.net/note/delete/102528/integrated
Del: useless     -- https://master.php.net/note/delete/102528/useless
Del: bad code    -- https://master.php.net/note/delete/102528/bad+code
Del: spam        -- https://master.php.net/note/delete/102528/spam
Del: non-english -- https://master.php.net/note/delete/102528/non-english
Del: in docs     -- https://master.php.net/note/delete/102528/in+docs
Del: other reasons-- https://master.php.net/note/delete/102528
Reject      -- https://master.php.net/note/reject/102528
Search      -- https://master.php.net/manage/user-notes.php