时间:2021-07-01 10:21:17 帮助过:25人阅读
<?php
require_once('Benchmark/Iterate.php');
define('MAX_RUN',100);
$data = array(1, 2, 3, 4, 5);
doBenchmark('v1', $data);
doBenchmark('v2', $data);
doBenchmark('v3', $data);
function doBenchmark($functionName = null, $arr = null)
{
reset($arr);
$benchmark = new Benchmark_Iterate;
$benchmark->run(MAX_RUN, $functionName, $arr);
$result = $benchmark->get();
echo '<br>';
printf("%s ran %d times where average exec time %.5f ms",$functionName,$result['iterations'],$result['mean'] * 1000);
}
function v1($myArray = null) {
// 效率很差的循环
for ($i =0; $i < sizeof($myArray); $i )
{
echo '<!--' . $myArray[$i] . ' --> ';
}
}
function v2($myArray = null) {
// 效率略有提高
$max = sizeof($myArray);
for ($i =0; $i < $max ; $i )
{
echo '<!--' . $myArray[$i] . ' --> ';
}
}
function v3($myArray = null){
//最佳效率
echo "<!--", implode(" --> <!--", $myArray), " --> ";
}
?>