`

40个技巧优化你的PHP代码

    博客分类:
  • php
阅读更多
英文版权归Reinhold Weber所有,中译文作者yangyang(aka davidkoree)。双语版可用于非商业传播,但须注明英文版作者、版权信息,以及中译文作者。


1. If a method can be static, declare it static. Speed improvement is by a factor of  如果一个方法可静态化,就对它做静态声明。速率可提升至4倍。

2. echo is faster than print. echo 比 print 快。

3. Use echo’s multiple parameters instead of string concatenation. 使用echo的多   重参数(译注:指用逗号而不是句点)代替字符串连接。

4. Set the maxvalue for your for-loops before and not in the loop. 在执行for循环之前确定最大循环数,不要每循环一次都计算最大值。

5. Unset your variables to free memory, especially large arrays. 注销那些不用的变量尤其是大数组,以便释放内存。

6. Avoid magic like __get, __set, __autoload 尽量避免使用__get,__set,__autoload。

7. require_once() is expensive require_once()代价昂贵。

8. Use full paths in includes and requires, less time spent on resolving the OS paths. 在包含文件时使用完整路径,解析操作系统路径所需的时间会更少。

9. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time() 如果你想知道脚本开始执行(译注:即服务器端收到客户端请求)的时刻,使用$_SERVER[‘REQUEST_TIME’]要好于time()。

10. See if you can use strncasecmp, strpbrk and stripos instead of regex. 检查是否能用strncasecmp,strpbrk,stripos函数代替正则表达式完成相同功能。

11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4. str_replace函数比preg_replace函数快,但strtr函数的效率是str_replace函数的四倍。

12. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments. 如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行 代码接受数组作为查询和替换的参数。

13. It’s better to use select statements than multi if, else if, statements. 使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。

14. Error suppression with @ is very slow. 用@屏蔽错误消息的做法非常低效。

15. Turn on apache’s mod_deflate 打开apache的mod_deflate模块。

16. Close your database connections when you’re done with them. 数据库连接当使用完毕时应关掉。

17. $row[’id’] is 7 times faster than $row[id]. $row[‘id’]的效率是$row[id]的7倍。

18. Error messages are expensive. 错误消息代价昂贵。

19. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time. 尽量不要在for循环中使用函数,比如for ($x=0; $x < count($array); $x)每循环一次都会调用count()函数。

20. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function. 在方法中递增局部变量,速度是最快的。几乎与在函数中调用局部变量的速度相当。

21. Incrementing a global variable is 2 times slow than a local var. 递增一个全局变量要比递增一个局部变量慢2倍。

22. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable. 递增一个对象属性(如:$this->prop++)要比递增一个局部变量慢3倍。

23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。

24. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists. 仅定义一个局部变量而没在函数中调用它,同样会减慢速度(其程度相当于递增一个局部变量)。PHP大概会检查看是否存在全局变量。

25. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance. 方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都)添加了10个方法,但性能上没有变化。

26. Methods in derived classes run faster than ones defined in the base class. 派生类中的方法运行起来要快于在基类中定义的同样的方法。

27. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations. 调用带有一个参数的空函数,其花费的时间相当于执行7至8次的局部变量递增操作。类似的方法调用所花费的时间接近于15次的局部变量递增操作。

28. Surrounding your string by ‘ instead of " will make things interpret a little faster since php looks for variables inside "…" but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string. 用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会。当然,只有当你不需要在字符串中包含变 量时才可以这么做。

29. When echoing strings it’s faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments. 输出多个字符串时,用逗号代替句点来分隔字符串,速度更快。注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册 中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。

30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts. Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面,少用脚本。

31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times. 除非脚本可以缓存,否则每次调用时都会重新编译一次。引入一套PHP缓存机制通常可以提升25%至100%的性能,以免除编译开销。

32. Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request. 尽量做缓存,可使用memcached。memcached是一款高性能的内存对象缓存系统,可用来加速动态Web应用程序,减轻数据库负载。对运算码 (OP code)的缓存很有用,使得脚本不必为每个请求做重新编译。

33. When working with strings and you need to check that the string is either of a certain length you’d understandably would want to use the strlen() function. This function is pretty quick since it’s operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick. 当操作字符串并需要检验其长度是否满足某种要求时,你想当然地会使用strlen()函数。此函数执行起来相当快,因为它不做任何计算,只返回在zval 结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度。但是,由于strlen()是函数,多多少少会有些慢,因为函数调用会经过诸多步 骤,如字母小写化(译注:指函数名小写化,PHP不区分函数名大小写)、哈希查找,会跟随被调用的函数一起执行。在某些情况下,你可以使用isset() 技巧加速执行你的代码。
Ex.(举例如下)
if (strlen($foo) < 5) { echo "Foo is too short"; }
vs.(与下面的技巧做比较)
if (!isset($foo{5})) { echo "Foo is too short"; }
Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it’s execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string’s length. 调用isset()恰巧比strlen()快,因为与后者不同的是,isset()作为一种语言结构,意味着它的执行不需要函数查找和字母小写化。也就是 说,实际上在检验字符串长度的顶层代码中你没有花太多开销。

34. When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don’t go modifying your C or Java code thinking it’ll suddenly become faster, it won’t. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend’s PHP optimizer. It is still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer. 当执行变量$i的递增或递减时,$i++会比++$i慢一些。这种差异是PHP特有的,并不适用于其他语言,所以请不要修改你的C或Java代码并指望它 们能立即变快,没用的。++$i更快是因为它只需要3条指令(opcodes),$i++则需要4条指令。后置递增实际上会产生一个临时变量,这个临时变 量随后被递增。而前置递增直接在原值上递增。这是最优化处理的一种,正如Zend的PHP优化器所作的那样。牢记这个优化处理不失为一个好主意,因为并不 是所有的指令优化器都会做同样的优化处理,并且存在大量没有装配指令优化器的互联网服务提供商(ISPs)和服务器。

35. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory. 并不是事必面向对象(OOP),面向对象往往开销很大,每个方法和对象调用都会消耗很多内存。

36. Do not implement every data structure as a class, arrays are useful, too. 并非要用类实现所有的数据结构,数组也很有用。

37. Don’t split methods too much, think, which code you will really re-use. 不要把方法细分得过多,仔细想想你真正打算重用的是哪些代码?

38. You can always split the code of a method later, when needed. 当你需要时,你总能把代码分解成方法。

39. Make use of the countless predefined functions. 尽量采用大量的PHP内置函数。

40. If you have very time consuming functions in your code, consider writing them as C extensions. 如果在代码中存在大量耗时的函数,你可以考虑用C扩展的方式实现它们。

41. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview. 评估检验(profile)你的代码。检验器会告诉你,代码的哪些部分消耗了多少时间。Xdebug调试器包含了检验程序,评估检验总体上可以显示出代码 的瓶颈。

42. mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%. mod_zip可作为Apache模块,用来即时压缩你的数据,并可让数据传输量降低80%。

43. Excellent Article (http://phplens.com/lens/php-book/optimizing-debugging-php.php)about optimizing php by John Lim 另一篇优化PHP的精彩文章,由John Lim撰写。



分享到:
评论

相关推荐

    PHP优化,代码优化

    以下是从给定文件中提炼出的关于PHP优化的重要知识点,这些方法覆盖了代码层面、数据库交互、以及服务器配置等多个方面。 #### 代码优化策略 1. **静态方法声明**:将可以静态化的函数声明为静态,可以显著提高...

    40条优化php代码的小实例.

    以下40个小实例提供了关于如何优化PHP代码的实用技巧: 1. **静态方法优化**:尽可能地将方法声明为静态,这可以减少对象创建的开销,提高约25%的执行速度。 2. **echo vs print**:在输出字符串时,echo比print更...

    加密优化Linux下的PHP代码.pdf

    对于系统开发人员来说,理解如何在Linux环境中调试和优化PHP代码是至关重要的,这通常需要掌握一些常用的调试工具,如`gdb`,以及阅读和分析PHP的错误日志。 总之,“加密优化Linux下的PHP代码”涵盖的内容广泛,既...

    php 代码优化指南,给你的php加速

    ### PHP代码优化指南 在开发基于PHP的应用程序时,代码的执行效率直接影响着用户体验和服务器资源的消耗。本文档详细列举了可能导致PHP代码性能下降的一些常见问题及其优化建议,适用于不同水平的PHP开发者。 ####...

    让你的程序跑的更快,php代码优化

    本文将详细介绍一系列有效的PHP代码优化技巧,帮助你的PHP程序更加高效地运行。 #### 1. 避免不必要的字符串操作 在处理字符串时,尽量避免使用`echo`和`print`进行多次输出,因为这会导致额外的缓冲区操作。如果...

    18个网站优化技巧

    ### 18个网站优化技巧:首页响应慢与首页速度优化 #### 一、引言 随着互联网技术的发展,用户对网页加载速度的要求越来越高。快速的页面加载不仅能够提升搜索引擎排名,还能显著改善网站转化率及用户体验。然而,...

    PHP深度分析:101个核心技巧、窍门和问题解决方法 + 源代码

    《PHP深度分析:101个核心技巧、窍门和问题解决方法》是一本专为PHP开发者准备的进阶教程,旨在帮助读者深入理解PHP语言并掌握一系列实用的编程技巧。书中结合源代码实例,提供了丰富的实践指导,涵盖从基础到高级的...

    40条优化php代码的小实例

    标题与描述中的知识点聚焦于PHP代码优化技巧,旨在提升代码执行效率、节省资源及改进编程实践。以下是对这些技巧的详细解析: 1. **静态方法的使用**:将合适的方法声明为静态,可以提高约四分之一的速度。静态方法...

    PHP深度分析:101个核心技巧、窍门和问题解决方法(原书第2版).pdf+PHP 内核文档.chm

    《PHP深度分析:101个核心技巧、窍门和问题解决方法》是PHP开发者的一本宝贵资源,这本书深入探讨了PHP编程中的诸多关键概念和技术,旨在帮助读者提升技能,解决实际开发中遇到的问题。结合《PHP 内核文档》CHM格式...

    php 的十个高级技巧

    在PHP编程语言的世界里,掌握一些高级技巧可以显著提升代码质量、效率以及开发效率。以下就是十个值得新手学习的PHP高级技巧: ...在实际开发中不断实践和理解这些技巧,将有助于编写出更高效、更健壮的PHP代码。

    百度全自动推送文章收录PHP代码附带教程

    "百度全自动推送文章收录PHP代码附带教程"是一个帮助网站管理员实现快速文章收录的解决方案,通过编写PHP代码实现与百度搜索引擎的自动交互。 首先,我们要理解什么是“百度自动推送”。这是一种技术手段,允许网站...

    php优化知识集锦

    "php优化知识集锦"这个压缩包显然包含了丰富的资料,旨在帮助开发者掌握和应用各种PHP优化技巧。以下是一些核心的PHP优化知识点: 1. **代码优化** - **避免使用短标签**:虽然PHP短标签`和`简洁易读,但它们的...

    PHP精粹编写高效PHP代码 书本源码

    《PHP精粹编写高效PHP代码》是一本专为PHP开发者设计的高级教程,旨在帮助读者提升编程技巧,编写出性能更优、效率更高的代码。这本书深入探讨了PHP语言的各个方面,包括最佳实践、优化策略以及如何避免常见的性能...

    php调试与优化的小技巧.doc

    总的来说,PHP的调试和优化是一个持续的过程,需要结合各种工具和技术,理解代码执行的细节,以及对性能瓶颈的深入分析。不断学习和实践,可以提升PHP开发者的技能,打造出更稳定、高效的PHP应用。

    PHP精粹 编写高效PHP代码pdf

    《PHP精粹 编写高效PHP代码》这本书是PHP开发者的重要参考资料,旨在帮助读者提升PHP编程技巧,优化代码性能,从而编写出更高效、更可靠的PHP应用。在深入理解PHP语言特性和最佳实践的基础上,我们可以从以下几个...

    PHP执行速率优化技巧小结

    以下是一些关于PHP执行速率优化的技巧和建议: ...通过遵循这些最佳实践,你可以显著提高PHP代码的执行效率,从而优化整体应用程序性能。记得在优化时始终权衡代码的可读性和维护性,因为这两方面同样重要。

    PHP性能优化方式

    总的来说,PHP性能优化是一个综合性的过程,需要结合代码、架构和工具等多方面因素,持续监控和调整,以实现最佳的运行效率。在实际工作中,开发者应该养成良好的编程习惯,不断学习新的优化技巧,以应对日益复杂的...

    PHP程序加速探索之代码优化

    总的来说,PHP代码优化是一个涉及多方面的过程,包括但不限于循环优化、函数选择、数据库查询效率和良好的编程习惯。通过持续学习和实践,开发者可以逐步掌握这些技巧,提升自己的代码质量,为用户提供更加流畅的...

    一个使用 php 语言的 TSPL 标签打印机实现_PHP_代码_下载

    在"tspl-printer-php-master"这个压缩包中,很可能是包含了一个PHP库或者示例代码,用于处理与TSPL标签打印机的交互。这个库可能提供了以下功能: 1. **TSPL指令生成**:提供了一系列函数或类,帮助开发者方便地...

Global site tag (gtag.js) - Google Analytics