英文版权归Reinhold Weber所有,中译文作者yangyang(aka davidkoree)。双语版可用于非商业传播,但须注明英文版作者、版权信息,以及中译文作者。翻译水平有限,请广大PHPer指正。
1. If a method can be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化,就对它做静态声明。速率可提升至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,
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