原文引用:40 Tips for optimizing your php code
簡體引用:优化 PHP 代码的 40 条建议
繁體引用:PHP 程式效能優化的 40 條建議
無意間翻到的,我稍微做了一下我個人認為差異性比較多的部分。
繁體中文引用備份
- 如果一個方法可靜態化,就對它做靜態聲明,速率可提升至4倍
echo
比print
快- 使用
echo
的多重參數(譯註:指用逗號而不是句點)代替字元串連接 - 在執行
for
循環之前確定最大循環數,不要每循環一次都計算最大值 - 註銷那些不用的變量尤其是大數組,以便釋放內存
- 盡量避免使用
__get
,__set
,__autoload
require_once()
代價昂貴- 在包含文件時使用完整路徑,解析操作系統路徑所需的時間會更少
- 如果你想知道腳本開始執行(譯註:即服務器端收到客戶端請求)的時刻,使用
$_SERVER['REQUEST_TIME']
要好於time()
- 函數代替正則表達式完成相同功能
str_replace
函數比preg_replace
函數快,但strtr
函數的效率是str_replace
函數的四倍- 如果一個字元串替換函數,可接受數組或字元作為參數,並且參數長度不太長,那麼可以考慮額外寫一段替換程式,使得每次傳遞參數是一個字元,而不是只寫一行程式接受數組作為查詢和替換的參數
- 使用選擇分支語句(譯註:即
switch case
)好於使用多個if
,else if
語句 - 用
@
屏蔽錯誤消息的做法非常慢 - 打開 Apache 的
mod_deflate
模塊 - 資料庫連接當使用完畢時應關掉
$row['id']
的效率是$row[id]
的 7 倍- 錯誤訊息代價昂貴
- 盡量不要在
for
循環中使用函數,比如for ($x=0; $x < count($array); $x)
每循環一次都會調用count()
函數 - 在 function 中遞增局部變量,速度是最快的。幾乎與在函數中調用局部變量的速度相當
- 遞增一個全局變量要比遞增一個局部變量慢 2 倍
- 遞增一個物件屬性(如:
$this->prop++
)要比遞增一個局部變量慢 3 倍 - 遞增一個未預定義的局部變量要比遞增一個預定義的局部變量慢 9 至 10 倍
- 僅定義一個局部變量而沒在函數中調用它,同樣會減慢速度(其程度相當於遞增一個局部變量)。PHP 大概會檢查看是否存在全局變量
- 方法調用看來與類中定義的方法的數量無關,因為我(在測試方法之前和之後都)添加了 10 個方法,但性能上沒有變化
- 派生類中的方法運行起來要快於在基類中定義的同樣的方法
- 調用帶有一個參數的空函數,其花費的時間相當於執行 7 至 8 次的局部變量遞增操作。類似的方法調用所花費的時間接近於 15 次的局部變量遞增操作
- 用單引號代替雙引號來包含字元串,這樣做會更快一些。因為 PHP 會在雙引號包圍的字元串中搜尋變量,單引號則不會。當然,只有當你不需要在字元串中包含變量時才可以這麼做
- 輸出多個字元串時,用逗號代替句點來分隔字元串,速度更快。註意:只有
echo
能這麼做,它是一種可以把多個字元串當作參數的“函數”(譯註:PHP 手冊中說echo
是語言結構,不是真正的函數,故把函數加上了雙引號) - Apache 解析一個 PHP 腳本的時間要比解析一個靜態 HTML 頁面慢 2 至 10 倍。盡量多用靜態 HTML 頁面,少用腳本
- 除非腳本可以緩存,否則每次調用時都會重新編譯一次。引入一套 PHP 緩存機制通常可以提升 25% 至 100% 的性能,以免除編譯開銷
- 盡量做緩存,可使用 memcached。memcached 是一款高性能的內存物件緩存系統,可用來加速動態Web應用程式,減輕數據庫負載。對運算碼 (OP code)的緩存很有用,使得腳本不必為每個請求做重新編譯
- 當操作字元串並需要檢驗其長度是否滿足某種要求時,你想當然地會使用
strlen()
函數。此函數執行起來相當快,因為它不做任何計算,只返回在zval 結構(C 的內置數據結構,用於存儲 PHP 變量)中存儲的已知字元串長度。但是,由於strlen()
是函數,多多少少會有些慢,因為函數調用會經過諸多步驟,如字母小寫化(譯註:指函數名小寫化,PHP 不區分函數名大小寫),會跟隨被調用的函數一起執行。在某些情況下,你可以使用isset()
技巧加速執行你的程式 舉例如下:if (strlen($foo) < 5) { echo "Foo is too short"; }
與下面的技巧做比較:if (!isset($foo{5})) { echo "Foo is too short"; }
調用isset()
恰巧比strlen()
快,因為與後者不同的是,isset()
作為一種語言結構,意味著它的執行不需要函數查找和字母小寫化。也就是說,實際上在檢驗字元串長度的頂層程式中你沒有花太多開銷 - 當執行變數
$i
的遞增或遞減時,$i++
會比++$i
慢一些。這種差異是 PHP 特有的,並不適用於其他語言,所以請不要修改你的 C 或 Java 程式並指望它們能立即變快,沒用的。++$i
更快是因為它只需要 3 條指令(opcodes),$i++
則需要 4 條指令。後置遞增實際上會產生一個臨時變量,這個臨時變量隨後被遞增。而前置遞增直接在原值上遞增。這是最優化處理的一種,正如 Zend 的 PHP 優化器所作的那樣。牢記這個優化處理不失為一個好主意,因為並不是所有的指令優化器都會做同樣的優化處理,並且存在大量沒有裝配指令優化器的網際網路服務提供商(ISPs)和服務器 - 並不是所有都要使用 OOP,面向物件往往開銷很大,每個方法和物件調用都會消耗很多 Memory
- 並非要用 class 實現所有的數據結構,數組也很有用
- 不要把方法細分得過多,仔細想想你真正打算重用的是哪些程式?
- 當你需要時,你總能把程式分解成方法
- 盡量採用大量的 PHP 內置函數
- 如果在程式中存在大量耗時的函數,你可以考慮用C擴展的方式實現它們
- 評估檢驗 (profile) 你的程式。檢驗器會告訴你,程式的哪些部分消耗了多少時間。Xdebug 調試器包含了檢驗程式,評估檢驗總體上可以顯示出程式的瓶頸
mod_zip
可作為 Apache 模塊,用來即時壓縮你的數據,並可讓數據傳輸量降低 80%- 另一篇 優化 PHP 的精彩文章,由 John Lim 撰寫
原文引用備份 (英文)
- If a method can be static, declare it static. Speed improvement is by a factor of 4.
echo
is faster thanprint
.- Use echo’s multiple parameters instead of string concatenation.
- Set the maxvalue for your for-loops before and not in the loop.
- Unset your variables to free memory, especially large arrays.
- Avoid magic like
__get
,__set
,__autoload
require_once()
is expensive- 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 totime()
- See if you can use strncasecmp, strpbrk and stripos instead of regex
str_replace
is faster thanpreg_replace
, butstrtr
is faster thanstr_replace
by a factor of 4- 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.
- Error suppression with
@
is very slow. - Turn on apache’s
mod_deflate
- Close your database connections when you’re done with them
$row[’id’]
is 7 times faster than$row[id]
- Error messages are expensive
- Do not use functions inside of for loop, such as
for ($x=0; $x < count($array); $x)
Thecount()
function gets called each time. - 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 2 times slow than a local var.
- Incrementing an object property (eg.
$this->prop++
) is 3 times slower than a local variable. - Incrementing an undefined local variable is 9-10 times slower than a pre-initialized 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.
- 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.
- 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 7-8 $localvar++ operations. A similar method call is of course about 15 $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.
- 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.
- 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.
- 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.
- 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
- 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. Ex.if (strlen($foo) < 5) { echo "Foo is too short"; }
vs.if (!isset($foo{5})) { echo "Foo is too short"; }
Callingisset()
happens to be faster thenstrlen()
because unlikestrlen()
,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. - 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 a 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. - Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory.
- 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 re-use
- You can always split the code of a method later, when needed
- Make use of the countless predefined functions
- If you have very time consuming functions in your code, consider writing them as C extensions
- 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
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%- Excellent Article about optimizing php by John Lim