首頁 > 後端開發 > php教程 > 冬蟲夏草的功效與作用及食用方法 優化PHP程序的方法小結

冬蟲夏草的功效與作用及食用方法 優化PHP程序的方法小結

WBOY
發布: 2016-07-29 08:48:08
原創
1022 人瀏覽過

1. If a method c++an 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, $_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 ew your. 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   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 tested 10 more methods to the test class (before and after the test method) with no 方法 in the test class (before and after the test method) with nochange informance informance.的方法的數量無關,因為我(在測試方法之前和之後都)添加了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 $lovar 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 …" but not inside '…'. Of course can in the string. 用單引號代替雙引號來包含字串,這樣做會更快一些。字串中包含變數時才可以這麼做。當字串時,用逗號代替句點來分隔字串,速度更快。 echo是語言結構,不是真正的函數,故把函數加上了雙引號)。 pages and fewer scripts. Apache解析一個PHP腳本的時間要比解析一個靜態HTML頁面慢2至10倍。 . 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 是ed 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 sperat since culace sintty formly notat is isat isatform​​i ​​is is 捲髮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 said function. ) trick. 當運算字串並需要檢驗其長度是否滿足某種要求時,你想當然地會使用strlen()函數。此函數執行起來相當快,因為它不做任何計算,只傳回在zval結構(C的內建資料結構,用於儲存PHP變數)中儲存的已知字串長度。但是,由於strlen()是函數,多多少少會有些慢,因為函數調用會經過諸多步驟,如字母小寫化(譯註:指函數名小寫化,PHP不區分函數名大小寫)、哈希查找,會跟隨被呼叫的函數一起執行。在某些情況下,你可以使用isset()技巧來加速執行你的程式碼。
  Ex.(舉例如下)
  if (strlen($foo)   vs.(與下面的技巧做比較)
 )) { 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 language construct and not a function mean that 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 langd unges, Javao't amk un​​k un​​k un​​koog mow ununun or muaing 遊戲, Java sooo Java 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 a ly need 3. Post incrementation actually causes in the creation of that temporary then 產生。 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 bdebug debugger already contains a profiler. Profiling shows you the bottbug debugger already contains a profiler. Profiling shows you the bottcks in viewview. 評估你的檢驗代碼。檢驗器會告訴你,程式碼的哪些部分消耗了多少時間。 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可作為Apacheuce the data to transfer up to 80%. mod_zip可作為Apacheuce the data to transfer up to 80%. mod_zip可作為Apacheuce the data to transfer up to 80%. mod_zip可作為Apache 模組,用來傳輸時可壓縮資料量80%.

以上就介紹了冬蟲夏草的功效與作用及食用方法 優化PHP程序的方法小結,包括了冬蟲夏草的功效與作用及食用方法方面的內容,希望對PHP教程有興趣的朋友有所幫助。

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板