note 105114 added to function.eval
| From: | jdomin at gmail dot com | Date: | Thu, 28 Jul 2011 14:57:24 +0000 |
| Subject: | note 105114 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181957@lists.php.net to get a copy of this message | ||
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";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.35.68.220
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit -- https://master.php.net/note/edit/105114
Del: integrated -- https://master.php.net/note/delete/105114/integrated
Del: useless -- https://master.php.net/note/delete/105114/useless
Del: bad code -- https://master.php.net/note/delete/105114/bad+code
Del: spam -- https://master.php.net/note/delete/105114/spam
Del: non-english -- https://master.php.net/note/delete/105114/non-english
Del: in docs -- https://master.php.net/note/delete/105114/in+docs
Del: other reasons-- https://master.php.net/note/delete/105114
Reject -- https://master.php.net/note/reject/105114
Search -- https://master.php.net/manage/user-notes.php