note 57492 added to function.eval

From: Date: Wed, 05 Oct 2005 20:46:25 +0000
Subject: note 57492 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-96454@lists.php.net to get a copy of this message
Didn't trust your benchmark, so I reverted it and added some more floating point instructions etc. (thought perhaps there was some error how you counted your time) But I can confirm that test 3 was the fastest. <?php // straight PHP function control($n) { $ref = 0; while ($n-- > 0) { $ref += ($n % 2) ? 1 : ($n/($ref+1)); } return $ref; } // eval with return function testEval1($n) { $ref = 0; while ($n-- > 0) { $ref += eval('return ($n % 2) ? 1 : ($n/($ref+1));'); } return $ref; } // eval with inline scalar assignment function testEval2($n) { $ref = 0; while ($n-- > 0) { eval('$ref += ($n % 2) ? 1 : ($n/($ref+1));'); } return $ref; } // eval with while loop inlined function testEval3($n) { $ref = 0; eval('while ($n-- > 0) { $ref += ($n % 2) ? 1 : ($n/($ref+1)); }'); return $ref; } echo "<!DOCTYPE html PUBLIC \\"-//W3C//DTD XHTML 1.0 Transitional//EN\\"\\n". "\\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\">\\n". "\\n". "<html xmlns=\\"http://www.w3.org/1999/xhtml\\">\\n". "\\t<head>\\n". "\\t\\t<title>Benchmark Eval</title>\\n". "\\t\\t<style type=\\"text/css\\">\\n". "\\t\\t\\ttable {width: 40em; border: 1px dotted gray; border-collapse: collapse;}\\n". "\\t\\t\\ttd, th {border: 1px solid gray; }\\n". "\\t\\t\\tth {text-align: left; background: #ccccff;}\\n". "\\t\\t\\tcol {background: #eeeeee;}\\n". "\\t\\t\\tcol:first-child {background: #ffeeee;}\\n". "\\t\\t</style>\\n". "\\t</head>\\n". "\\t<body>\\n". "\\t\\t<table>\\n". "\\t\\t\\t<colgroup><col /><col span=\\"2\\"/></colgroup>\\n". "\\t\\t\\t<thead><tr><th>function</th><th>result</th><th>time (microseconds)</th></tr></thead>\\n". "\\t\\t\\t<tbody>\\n". ""; define(N, 0x1ffff); $tb = microtime(true); $r = control(N); $ta = microtime(true); echo "\\t\\t\\t\\t<tr><td>control</td><td>$r</td><td>".($tb - $ta)."</td></tr>\\n"; $tb = microtime(true); $r = testEval1(N); $ta = microtime(true); echo "\\t\\t\\t\\t<tr><td>testEval1</td><td>$r</td><td>".($tb - $ta)."</td></tr>\\n"; $tb = microtime(true); $r = testEval2(N); $ta = microtime(true); echo "\\t\\t\\t\\t<tr><td>testEval2</td><td>$r</td><td>".($tb - $ta)."</td></tr>\\n"; $tb = microtime(true); $r = testEval3(N); $ta = microtime(true); echo "\\t\\t\\t\\t<tr><td>testEval3</td><td>$r</td><td>".($tb - $ta)."</td></tr>\\n"; echo "\\t\\t\\t</tbody>\\n\\t\\t</table>\\n\\t</body>\\n</html>\\n"; ?> ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57492 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57492&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57492&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57492&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57492&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57492&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57492&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57492&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57492&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#96454) next »