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

Reading number is top 10 articles
GridView隐藏列取值解决方案_[Asp.Net教程]
asp.net2.0服务器控件之CheckBox控件
Linux下PHP连接MS,SQLServer的办法_php资料_编程技术
C#网络应用编程基础练习题与答案(九)_.net资料_编程技术
技巧:PHP关于中文汉字替换与模式匹配的问题_php资料_编程技术
用shtml来include网页文件(须开启SSI)_[Html教程]
ASP.NET技巧:HTTP性能调优之设置连接失效时间_.net资料_编程技术
C#网络应用编程基础练习题与答案(四)_.net资料_编程技术
PHP.MVC的模板标签系统之标签行为调度_php资料_编程技术
C#中国身份证验证(包括省份验证和校验码验证,符合GB11643-1999标准)_[Asp.Net教程]
Reading number is top 10 pictures
Men don't mature ten sign
什么叫国家
BingBingFan apple dew point photo gallery4
Magnificent cloud2
Exquisite decoration is not paying too much3
29 the belle stars after bath figure2
007 James. bond's new lover
The money of more than 100 countries and regions20
再发两张抽象画
乳娘帕梅拉安德森5
Download software ranking
The Bermuda triangle2
The cock of the Grosvenor LTD handsome
I'm come from Beijing1
Unix video tutorial11
Tram sex maniac 2 (H) rar bag17
Boxer vs Yellow4
Boxer's Top ten classic battle2
Ashlynn Video2
Boxer vs Yellow1
Boxer's Top ten classic battle1
aaa published in(发表于) 2013/12/24 5:56:41 Edit(编辑)
技巧:PHP开发网站程序代码的优化方法_php资料_编程技术

技巧:PHP开发网站程序代码的优化方法_php资料_编程技术

技巧:PHP开发网站程序代码的优化方法_php资料_编程技术-你的首页-uuhomepage.com

  如何消灭或优化那PHP开发网站程序的代码呢?

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

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

  webjx.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.