note 98760 added to function.eval

From: Date: Mon, 05 Jul 2010 20:35:24 +0000
Subject: note 98760 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-170722@lists.php.net to get a copy of this message
I was curious about plain code versus eval versus include, so I wrote the following script. Based on my results, it seems as though eval is the slowest method of the three, even though previous comments indicate otherwise. <?php /* Results Test #1: Plain Code Results: 0.0162778673172 Test #2: eval'd code Results: 0.0177816636562 Test #3: Included code Results: 0.0167677741051 */ echo "Test #1: Plain Code<br />"; $totalTime = 0; $totalAttempts = 0; for ($j = 0; $j < 1000; $j++) { $start = microtime(true); $i = 0; for ($k = 0; $k < 100000; $k++) { $i++; } $end = microtime(true); $elapsed = $end - $start; $totalTime += $elapsed; $totalAttempts++; } echo "Results: " . ($totalTime / $totalAttempts) . "<br />"; echo "Test #2: eval'd code<br />"; $totalTime = 0; $totalAttempts = 0; for ($j = 0; $j < 1000; $j++) { $start = microtime(true); eval('$i = 0; for ($k = 0; $k < 100000; $k++) { $i++; }'); $end = microtime(true); $elapsed = $end - $start; $totalTime += $elapsed; $totalAttempts++; } echo "Results: " . ($totalTime / $totalAttempts) . "<br />"; echo "Test #3: Included code<br />"; $totalTime = 0; $totalAttempts = 0; for ($j = 0; $j < 1000; $j++) { $start = microtime(true); include("evalme.php"); $end = microtime(true); $elapsed = $end - $start; $totalTime += $elapsed; $totalAttempts++; } echo "Results: " . ($totalTime / $totalAttempts) . "<br />"; ?> evalme.php <?php $i = 0; for ($k = 0; $k < 100000; $k++) { $i++; } ?> ---- Server IP: 72.55.140.6 Probable Submitter: 216.191.234.70 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/98760 Del: integrated -- https://master.php.net/note/delete/98760/integrated Del: useless -- https://master.php.net/note/delete/98760/useless Del: bad code -- https://master.php.net/note/delete/98760/bad+code Del: spam -- https://master.php.net/note/delete/98760/spam Del: non-english -- https://master.php.net/note/delete/98760/non-english Del: in docs -- https://master.php.net/note/delete/98760/in+docs Del: other reasons-- https://master.php.net/note/delete/98760 Reject -- https://master.php.net/note/reject/98760 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170722) next »