note 57492 deleted from function.eval by sniper
| From: | sniper@php.net | Date: | Thu, 29 Dec 2005 09:16:01 +0000 |
| Subject: | note 57492 deleted from function.eval by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100921@lists.php.net to get a copy of this message | ||
Note Submitter: azatoth at gmail dot com
----
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";
?>