Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)归海一刀published in(发表于) 2014/2/10 6:49:34 PHP程序加速探索之代码优化_[PHP教程]
<?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), " --> "; } ?>
赞