If a method can be static declare it static Speed improvement is by a factor of 如果一个方法可静态化就对它做静态声明速率可提升至倍
echo is faster than print echo 比 print 快
Use echo’s multiple parameters instead of string concatenation 使用echo的多重参数(译注指用逗号而不是句点)代替字符串连接
Set the maxvalue for your forloops before and not in the loop 在执行for循环之前确定最大循环数不要每循环一次都计算最大值
Unset your variables to free memory especially large arrays 注销那些不用的变量尤其是大数组以便释放内存
Avoid magic like __get __set __autoload 尽量避免使用__get__set__autoload
require_once() is expensive require_once()代价昂贵
Use full paths in includes and requires less time spent on resolving the OS paths 在包含文件时使用完整路径解析操作系统路径所需的时间会更少
If you need to find out the time when the script started executing $_SERVER[’REQUEST_TIME’] is preferred to time() 如果你想知道脚本开始执行(译注即服务器端收到客户端请求)的时刻使用$_SERVER[‘REQUEST_TIME’]要好于time()
See if you can use strncasecmp strpbrk and stripos instead of regex 检查是否能用strncasecmpstrpbrkstripos函数代替正则表达式完成相同功能
str_replace is faster than preg_replace but strtr is faster than str_replace by a factor of str_replace函数比preg_replace函数快但strtr函数的效率是str_replace函数的四倍
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 如果一个字符串替换函数可接受数组或字符作为参数并且参数长度不太长那么可以考虑额外写一段替换代码使得每次传递参数是一个字符而不是只写一行 代码接受数组作为查询和替换的参数
It’s better to use select statements than multi if else if statements 使用选择分支语句(译注即switch case)好于使用多个ifelse if语句
Error suppression with @ is very slow 用@屏蔽错误消息的做法非常低效
Turn on apache’s mod_deflate 打开apache的mod_deflate模块
Close your database connections when you’re done with them 数据库连接当使用完毕时应关掉
$row[’id’] is times faster than $row[id] $row[‘id’]的效率是$row[id]的倍
Error messages are expensive 错误消息代价昂贵
Do not use functions inside of for loop such as for ($x=; $x < count($array); $x) The count() function gets called each time 尽量不要在for循环中使用函数比如for ($x=; $x < count($array); $x)每循环一次都会调用count()函数
Incrementing a local variable in a method is the fastest Nearly the same as calling a local variable in a function 在方法中递增局部变量速度是最快的几乎与在函数中调用局部变量的速度相当
Incrementing a global variable is times slow than a local var 递增一个全局变量要比递增一个局部变量慢倍
Incrementing an object property (eg $this>prop++) is times slower than a local variable 递增一个对象属性(如$this>prop++)要比递增一个局部变量慢倍
Incrementing an undefined local variable is times slower than a preinitialized one 递增一个未预定义的局部变量要比递增一个预定义的局部变量慢至倍
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大概会检查看是否存在全局变量
Method invocation appears to be independent of the number of methods defined in the class because I added more methods to the test class (before and after the test method) with no change in performance 方法调用看来与类中定义的方法的数量无关因为我(在测试方法之前和之后都)添加了个方法但性能上没有变化
Methods in derived classes run faster than ones defined in the base class 派生类中的方法运行起来要快于在基类中定义的同样的方法
A function call with one parameter and an empty function body takes about the same time as doing $localvar++ operations A similar method call is of course about $localvar++ operations 调用带有一个参数的空函数其花费的时间相当于执行至次的局部变量递增操作类似的方法调用所花费的时间接近于次的局部变量递增操作
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会在双引号包围的字符串中搜寻变量单引号则不会当然只有当你不需要在字符串中包含变 量时才可以这么做
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是语言结构不是真正的函数故把函数加上了双引号)
A PHP script will be served at least times slower than a static HTML page by Apache Try to use more static HTML pages and fewer scripts Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢至倍尽量多用静态HTML页面少用脚本
Your PHP scripts are recompiled every time unless the scripts are cached Install a PHP caching product to typically increase performance by % by removing compile times 除非脚本可以缓存否则每次调用时都会重新编译一次引入一套PHP缓存机制通常可以提升%至%的性能以免除编译开销
Cache as much as possible Use memcached memcached is a highperformance 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 尽量做缓存可使用memcachedmemcached是一款高性能的内存对象缓存系统可用来加速动态Web应用程序减轻数据库负载对运算码(OP code)的缓存很有用使得脚本不必为每个请求做重新编译
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) < ) { echo "Foo is too short"; }
vs(与下面的技巧做比较)
if (!isset($foo{})) { 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()作为一种语言结构意味着它的执行不需要函数查找和字母小写化也就是 说实际上在检验字符串长度的顶层代码中你没有花太多开销
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 opcodes used for $i++ you only need Post incrementation actually causes in the creation of a temporary var that is then incremented While preincrementation 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更快是因为它只需要条指令(opcodes)$i++则需要条指令后置递增实际上会产生一个临时变量这个临时变 量随后被递增而前置递增直接在原值上递增这是最优化处理的一种正如Zend的PHP优化器所作的那样牢记这个优化处理不失为一个好主意因为并不 是所有的指令优化器都会做同样的优化处理并且存在大量没有装配指令优化器的互联网服务提供商(ISPs)和服务器
Not everything has to be OOP often it is too much overhead each method and object call consumes a lot of memory 并不是事必面向对象(OOP)面向对象往往开销很大每个方法和对象调用都会消耗很多内存
Do not implement every data structure as a class arrays are useful too 并非要用类实现所有的数据结构数组也很有用
Don’t split methods too much think which code you will really reuse 不要把方法细分得过多仔细想想你真正打算重用的是哪些代码?
You can always split the code of a method later when needed 当你需要时你总能把代码分解成方法
Make use of the countless predefined functions 尽量采用大量的PHP内置函数
If you have very time consuming functions in your code consider writing them as C extensions 如果在代码中存在大量耗时的函数你可以考虑用C扩展的方式实现它们
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调试器包含了检验程序评估检验总体上可以显示出代码的瓶颈
mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to % mod_zip可作为Apache模块用来即时压缩你的数据并可让数据传输量降低%