您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文


2022-06-13   來源: PHP編程 

   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() 技巧加速執行你的代碼

if (strlen($foo) < ) { echo "Foo is too short"; }
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模塊用來即時壓縮你的數據並可讓數據傳輸量降低%

    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.