All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
SQLServer的连接处理技巧_[SQL,Server教程]
HTML语言剖析(十五)调色原理_[Html教程]
asp.net2.0标准服务器控件典型应用
C#中二维数组的声明
HTML网页制作基础教程(1):认识HTML_[Html教程]
SQL Server2000连接中三个最常见错误分析_[SQL Server教程]
技巧:ASP.NET里的Session详细解释_.net资料_编程技术
Apache2,+,PHP,在Windows2000下不稳定解决方案_php资料_编程技术
JS获取网页中HTML元素的几种方法分析_[Html教程]
分清SQL,Server中易混淆的数据类型_[SQL,Server教程]
Reading number is top 10 pictures
小学生考试又现神作--还有外国的
Embarrassing things comic collection1
2015中美最新武器装备巅峰对决!
The money of more than 100 countries and regions12
China railway shunting skills competition
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line
Chinese paper-cut grilles art appreciation2
银行20年后可能消失
Sora aoi on twitter1
陪睡门马睿菈自曝写真 称首拍大尺度照片3
Download software ranking
Unix video tutorial6
Rio big adventure
好色的外科大夫
Boxer vs Yellow1
Proficient in JavaScript
WebService在.NET中的实战应用教学视频 → 第1集
Visual C++界面编程技术
The Bermuda triangle2
WebService在.NET中的实战应用教学视频 → 第5集
Tram sex maniac 2 (H) rar bag3
归海一刀 published in(发表于) 2014/2/10 6:45:34 Edit(编辑)
PHP 开发程序加速运行探索之慢代码优化方法_[PHP教程]

PHP 开发程序加速运行探索之慢代码优化方法_[PHP教程]

PHP 开发程序加速运行探索之慢代码优化方法_[PHP教程]

掌握了PEAR::BenchMark,现在你已经知道如何测试你的代码,知道如何判断你的代码是快是慢,是哪一部份比较慢。那么接下来我要说的就是如何消灭或优化那部份慢的代码。

  这一点上我个人最主要的经验只有两点,一是消除错误的或低效的循环;二是优化数据库查询语句。其实还存在一些其它的优化细节,比如“str_replace比ereg_replace快”、“echo比print快”等等。这些我暂时都放在一边,稍后我会提到用缓存来对付过于频繁的IO。

  下面我们将三个功能相同,但程序写法不同的函数的效率(消耗的时间)进行对比。

  badloops.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), " --> ";
}

?>


  程序输出的结果大概是这样的:

  v1 ran 100 times where average exec time 0.18400 ms
  v2 ran 100 times where average exec time 0.15500 ms
  v3 ran 100 times where average exec time 0.09100 ms

  可以看到,函数的执行时间变少,效率上升。

  函数v1有个很明显的错误,每一次循环的时间,都需要调用sizeof()函数来计算。
函数v2则在循环外把myArray数组的元素个数存到max变量中,避免了每次循环都要计算数组的元素个数,所以效率提高了。函数v3的效率最高,利用了现成的函数,避免循环。

  这个例子只是给你一个感性的认识,明白什么是相对高效的代码。在实际开发中,我相信会有很多人会迷迷糊糊地写出很多低效率的代码。要把代码写得精炼而高效,恐怕需要时间去锤炼:-) 但这是另一个话题了,我们略过不谈。

  数据库应用基本上每个PHP程序都会用到,在实际开发中我发现最影响整个系统效率的就是数据库这部份。至于数据库的优化和数据查询语句的优化,在此限于篇幅不详细讨论。







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.