Bench php (quelques exemples issus de http://www.phpbench.com/) Revision 71c84dc2967d (Tue Dec 16 2008 at 17:23) - Diff Link to this snippet: https://friendpaste.com/76BNRU58JAwQiQaZaiDRXe Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419<?php// Initial Configurationglobal $aHash;#### Tests 1 pour foreach / for / while-list-each##function Test1_Start() { global $aHash; $i = 0; $tmp = ''; while($i < 1000) { $tmp .= 'a'; ++$i; } $aHash = array_fill(100000000000000000000000, 100, $tmp); unset($i, $tmp); return "foreach / for / while-list-each";}function Test1_1() { global $aHash; /* The Test */ $t = microtime(true); foreach($aHash as $val); $end = (microtime(true) - $t); return array('foreach($aHash as $val);'=>$end);}function Test1_2() { global $aHash; /* The Test */ $t = microtime(true); while(list(,$val) = each($aHash)); $end = (microtime(true) - $t); return array('while(list(,$val) = each($aHash));'=>$end);}function Test1_3() { global $aHash; /* The Test */ $t = microtime(true); foreach($aHash as $key => $val); $end = (microtime(true) - $t); return array('foreach($aHash as $key => $val);'=>$end);}function Test1_4() { global $aHash; /* The Test */ $t = microtime(true); while(list($key,$val) = each($aHash)); $end = (microtime(true) - $t); return array('while(list($key,$val) = each($aHash));'=>$end);}function Test1_5() { global $aHash; /* The Test */ $t = microtime(true); foreach($aHash as $key=>$val) $tmp[] = $aHash[$key]; $end = (microtime(true) - $t); return array('foreach($aHash as $key=>$val) $tmp[] = $aHash[$key];'=>$end);}// Test Sourcefunction Test1_6() { global $aHash; /* The Test */ $t = microtime(true); while(list($key) = each($aHash)) $tmp[] = $aHash[$key]; $end = (microtime(true) - $t); return array('while(list($key) = each($aHash)) $tmp[] = $aHash[$key];'=>$end);}function Test1_7() { global $aHash; /* The Test */ $t = microtime(true); foreach($aHash as $key[] => $val[]); $end = (microtime(true) - $t); return array('foreach($aHash as $key[] => $val[]);'=>$end);;}function Test1_8() { global $aHash; /* The Test */ $t = microtime(true); array_keys($aHash); array_values($aHash); $end = (microtime(true) - $t); return array('array_keys($aHash); array_values($aHash);'=>$end);}// Test Sourcefunction Test1_9() { global $aHash; /* The Test */ $t = microtime(true); $key = array_keys($aHash); $size = sizeOf($key); for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]]; $end = (microtime(true) - $t); return array('for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];'=>$end);}// Test Sourcefunction Test1_10() { global $aHash; /* The Test */ $t = microtime(true); $keys=$vals=array(); while(list($key,$val) = each($aHash)); {$keys[]=$key; $vals[]=$val;} $end = (microtime(true) - $t); return array('$keys=$vals=array(); while(list($key,$val) = each($aHash)) {$keys[]=$key; $vals[]=$val;}'=>$end);}// Variable Clean-upfunction Test1_End() { global $aHash; unset($aHash);}function Test2_Start(){Test1_Start(); return 'foreach() vs. for vs. while(list() = each()) ';}function Test2_End(){return Test1_Start();}function Test2_1() { global $aHash; /* The Test */ $t = microtime(true); foreach($aHash as $key=>$val) $aHash[$key] .= "a"; $end = (microtime(true) - $t); return array('foreach($aHash as $key=>$val) $aHash[$key] .= "a";'=>$end);}function Test2_2() { global $aHash; /* The Test */ $t = microtime(true); while(list($key) = each($aHash)) $aHash[$key] .= "a"; $end = (microtime(true) - $t); return array('while(list($key) = each($aHash)) $aHash[$key] .= "a";'=>$end);}function Test2_3() { global $aHash; /* The Test */ $t = microtime(true); $key = array_keys($aHash); $size = sizeOf($key); for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a"; $end = (microtime(true) - $t); return array('for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";'=>$end);}function Test3_Start(){ global $aHash; $i = 0; $tmp = ''; while($i < 10000) { $tmp .= 'a'; ++$i; } $aHash = array_fill(5, 100000, $tmp); unset($i, $tmp); return 'For-loop';}// Test Sourcefunction Test3_1() { global $x; /* The Test */ $t = microtime(true); $size = count($x); for ($i=0; $i<$size; $i++); $end = (microtime(true) - $t); return array('$size = count($x); for ($i=0; $i<$size; $i++);' => $end);}// Test Sourcefunction Test3_2() { global $x; /* The Test */ $t = microtime(true); for ($i=0; $i<count($x); $i++); $end = (microtime(true) - $t); return array('for ($i=0; $i<count($x); $i++);' => $end);}function Test3_3() { global $x; /* The Test */ $t = microtime(true); $size = sizeof($x); for ($i=0; $i<$size; $i++); $end = (microtime(true) - $t); return array('$size = sizeof($x); for ($i=0; $i<$size; $i++);' => $end);}function Test3_4() { global $x; /* The Test */ $t = microtime(true); for ($i=0; $i<sizeof($x); $i++); $end = (microtime(true) - $t); return array('for ($i=0; $i<sizeof($x); $i++);' => $end);}// Variable Clean-upfunction Test3_End() { global $x, $aHash; unset($x, $aHash);}// Echofunction Test11_Start(){return 'Echo / Print';}function Test11_1() { global $answer; /* The Test */ ob_start(); $t = microtime(true); while($i < 1000) { echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'; ++$i; } $end = microtime(true) - $t; ob_end_clean(); return array("echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa';" => $end);}function Test11_2() { global $answer; /* The Test */ ob_start(); $t = microtime(true); while($i < 1000) { echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa'; ++$i; } $end = microtime(true) - $t; ob_end_clean(); return array("echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa';" => $end);}function Test11_3() { global $answer; /* The Test */ ob_start(); $t = microtime(true); while($i < 1000) { print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'; ++$i; } $end = microtime(true) - $t; ob_end_clean(); return array("print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa';" => $end);}function Test11_4() { global $answer; /* The Test */ ob_start(); $t = microtime(true); while($i < 1000) { $a = 'aaaaaaa'; echo $a.$a.$a.$a; ++$i; } $end = microtime(true) - $t; ob_end_clean(); return array('$a'." = 'aaaaaaa'; echo ".'$a.$a.$a.$a;' => $end);}function Test11_5() { global $answer; /* The Test */ ob_start(); $t = microtime(true); while($i < 1000) { $a = 'aaaaaaa'; echo $a,$a,$a,$a; ++$i; } $end = microtime(true) - $t; ob_end_clean(); return array('$a'." = 'aaaaaaa'; echo ".'$a,$a,$a,$a;' => $end);}function Test11_6() { global $answer; /* The Test */ ob_start(); $t = microtime(true); while($i < 1000) { $a = 'aaaaaaa'; print $a.$a.$a.$a; ++$i; } $end = microtime(true) - $t; ob_end_clean(); return array('$a'." = 'aaaaaaa'; print ".'$a.$a.$a.$a;' => $end);}// Variable Clean-upfunction Test11_End() { global $answer; unset($answer);}echo ' <style> body{font-size:80%;} table{font-size:100%; margin-left:2em; text-align:left; border:1px solid #888; border-collapse:collapse;} tr.first th{background:#eee;} td{border:1px solid #888; padding:5px; text-align:right;} th{border:1px solid #888; padding:5px; } </style>';$arr = array(1, 2, 3, 11);while(list(,$a) = each($arr)){ $test = 'Test'.$a.'_'; $start = $test.'Start'; $end = $test.'End'; echo '<h1>'.$start().'</h1><table>'; echo '<tr class="first"><th>Opération</th><th>Temps</th><th>%</th></tr>'; $b=1; $tests = array(); while(function_exists($test.$b)){ $start(); $letest = $test.$b; list($cle,$val) = each($letest()); $tests[$cle] = $val; $end(); $b++; } $min = min(array_values($tests)); asort($tests); while (list($cle,$val) = each($tests)){ echo '<tr><th>'.htmlentities($cle).'</th><td>'.number_format($val, 8, ',', ' ').'</td><td>'.(int)($val / $min * 100).'</td></tr>'; } echo '</table>'; $a++;}echo '</table>';?>