2. echo is faster than print. echo 比 print 快。 " /> 116美女写真午夜电影z,免费无遮挡又黄又爽网站,免费观看桶机十分钟

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

優(yōu)化PHP程序的方法小結(jié)

1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 如果一個方法可靜態(tài)化,就對它做靜態(tài)聲明。速率可提升至4倍。

  2. echo is faster than print. echo 比 print 快。

  3. Use echo's multiple parameters instead of string concatenation. 使用echo的多重參數(shù)(譯注:指用逗號而不是句點(diǎn))代替字符串連接。

  4. Set the maxvalue for your for-loops before and not in the loop. 在執(zhí)行for循環(huán)之前確定最大循環(huán)數(shù),不要每循環(huán)一次都計(jì)算最大值。

  5. Unset your variables to free memory, especially large arrays. 注銷那些不用的變量尤其是大數(shù)組,以便釋放內(nèi)存。

  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. 在包含文件時使用完整路徑,解析操作系統(tǒng)路徑所需的時間會更少。

  9. If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time() 如果你想知道腳本開始執(zhí)行(譯注:即服務(wù)器端收到客戶端請求)的時刻,使用$_SERVER['REQUEST_TIME']要好于time()。

  10. See if you can use strncasecmp, strpbrk and stripos instead of regex. 檢查是否能用strncasecmp,strpbrk,stripos函數(shù)代替正則表達(dá)式完成相同功能。

  11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4. str_replace函數(shù)比preg_replace函數(shù)快,但strtr函數(shù)的效率是str_replace函數(shù)的四倍。

  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. 如果一個字符串替換函數(shù),可接受數(shù)組或字符作為參數(shù),并且參數(shù)長度不太長,那么可以考慮額外寫一段替換代碼,使得每次傳遞參數(shù)是一個字符,而不是只寫一行代碼接受數(shù)組作為查詢和替換的參數(shù)。

  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. 數(shù)據(jù)庫連接當(dāng)使用完畢時應(yīng)關(guān)掉。

  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循環(huán)中使用函數(shù),比如for ($x=0; $x < count($array); $x)每循環(huán)一次都會調(diào)用count()函數(shù)。

  20. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function. 在方法中遞增局部變量,速度是最快的。幾乎與在函數(shù)中調(diào)用局部變量的速度相當(dāng)。

  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. 遞增一個未預(yù)定義的局部變量要比遞增一個預(yù)定義的局部變量慢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. 僅定義一個局部變量而沒在函數(shù)中調(diào)用它,同樣會減慢速度(其程度相當(dāng)于遞增一個局部變量)。php大概會檢查看是否存在全局變量。

  25. 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. 方法調(diào)用看來與類中定義的方法的數(shù)量無關(guān),因?yàn)槲?在測試方法之前和之后都)添加了10個方法,但性能上沒有變化。

  26. Methods in derived classes run faster than ones defined in the base class. 派生類中的方法運(yùn)行起來要快于在基類中定義的同樣的方法。

 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 $localvar++ operations. 調(diào)用帶有一個參數(shù)的空函數(shù),其花費(fèi)的時間相當(dāng)于執(zhí)行7至8次的局部變量遞增操作。類似的方法調(diào)用所花費(fèi)的時間接近于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 this when you don't need to have variables in the string. 用單引號代替雙引號來包含字符串,這樣做會更快一些。因?yàn)?a href=/itjie/phpjishu/ target=_blank class=infotextkey>php會在雙引號包圍的字符串中搜尋變量,單引號則不會。當(dāng)然,只有當(dāng)你不需要在字符串中包含變量時才可以這么做。

  29. 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. 輸出多個字符串時,用逗號代替句點(diǎn)來分隔字符串,速度更快。注意:只有echo能這么做,它是一種可以把多個字符串當(dāng)作參數(shù)的"函數(shù)"(譯注:php手冊中說echo是語言結(jié)構(gòu),不是真正的函數(shù),故把函數(shù)加上了雙引號)。

  30. 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. Apache解析一個php腳本的時間要比解析一個靜態(tài)HTML頁面慢2至10倍。盡量多用靜態(tài)HTML頁面,少用腳本。

  31. 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. 除非腳本可以緩存,否則每次調(diào)用時都會重新編譯一次。引入一套php緩存機(jī)制通常可以提升25%至100%的性能,以免除編譯開銷。

  32. 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. 盡量做緩存,可使用memcached.memcached是一款高性能的內(nèi)存對象緩存系統(tǒng),可用來加速動態(tài)Web應(yīng)用程序,減輕數(shù)據(jù)庫負(fù)載。對運(yùn)算碼(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 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. 當(dāng)操作字符串并需要檢驗(yàn)其長度是否滿足某種要求時,你想當(dāng)然地會使用strlen()函數(shù)。此函數(shù)執(zhí)行起來相當(dāng)快,因?yàn)樗蛔鋈魏斡?jì)算,只返回在zval結(jié)構(gòu)(C的內(nèi)置數(shù)據(jù)結(jié)構(gòu),用于存儲php變量)中存儲的已知字符串長度。但是,由于strlen()是函數(shù),多多少少會有些慢,因?yàn)楹瘮?shù)調(diào)用會經(jīng)過諸多步驟,如字母小寫化(譯注:指函數(shù)名小寫化,php不區(qū)分函數(shù)名大小寫)、哈希查找,會跟隨被調(diào)用的函數(shù)一起執(zhí)行。在某些情況下,你可以使用isset()技巧加速執(zhí)行你的代碼。

  Ex.(舉例如下)

  if (strlen($foo) < 5) { echo "Foo is too short"; }

  vs.(與下面的技巧做比較)

  if (!isset($foo{5})) { 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. 調(diào)用isset()恰巧比strlen()快,因?yàn)榕c后者不同的是,isset()作為一種語言結(jié)構(gòu),意味著它的執(zhí)行不需要函數(shù)查找和字母小寫化。也就是說,實(shí)際上在檢驗(yàn)字符串長度的頂層代碼中你沒有花太多開銷。

  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 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 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. 當(dāng)執(zhí)行變量$i的遞增或遞減時,$i++會比++$i慢一些。這種差異是php特有的,并不適用于其他語言,所以請不要修改你的C或Java代碼并指望它們能立即變快,沒用的。++$i更快是因?yàn)樗恍枰?條指令(opcodes),$i++則需要4條指令。后置遞增實(shí)際上會產(chǎn)生一個臨時變量,這個臨時變量隨后被遞增。而前置遞增直接在原值上遞增。這是最優(yōu)化處理的一種,正如Zend的php優(yōu)化器所作的那樣。牢記這個優(yōu)化處理不失為一個好主意,因?yàn)椴⒉皇撬械闹噶顑?yōu)化器都會做同樣的優(yōu)化處理,并且存在大量沒有裝配指令優(yōu)化器的互聯(lián)網(wǎng)服務(wù)提供商(ISPs)和服務(wù)器

  35. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory. 并不是事必面向?qū)ο?OOP),面向?qū)ο笸_銷很大,每個方法和對象調(diào)用都會消耗很多內(nèi)存。

  36. Do not implement every data structure as a class, arrays are useful, too. 并非要用類實(shí)現(xiàn)所有的數(shù)據(jù)結(jié)構(gòu),數(shù)組也很有用。

  37. Don't split methods too much, think, which code you will really re-use. 不要把方法細(xì)分得過多,仔細(xì)想想你真正打算重用的是哪些代碼?

  38. You can always split the code of a method later, when needed. 當(dāng)你需要時,你總能把代碼分解成方法。

  39. Make use of the countless predefined functions. 盡量采用大量的php內(nèi)置函數(shù)。

  40. If you have very time consuming functions in your code, consider writing them as C extensions. 如果在代碼中存在大量耗時的函數(shù),你可以考慮用C擴(kuò)展的方式實(shí)現(xiàn)它們。

  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 bottlenecks in overview. 評估檢驗(yàn)(profile)你的代碼。檢驗(yàn)器會告訴你,代碼的哪些部分消耗了多少時間。Xdebug調(diào)試器包含了檢驗(yàn)程序,評估檢驗(yàn)總體上可以顯示出代碼的瓶頸。

  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可作為Apache模塊,用來即時壓縮你的數(shù)據(jù),并可讓數(shù)據(jù)傳輸量降低80%.

php技術(shù)優(yōu)化PHP程序的方法小結(jié),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 一级毛片美国 | 国产最新进精品视频 | 9277在线观看免费高清完整版 | 自拍偷拍2 | 拔萝卜电视剧高清免费 | 最新无码专区在线视频 | 欧美视频精品一区二区三区 | 97在线播放视频 | 99视频精品全部免费观看 | 草比比过程图 | 制服国产欧美亚洲日韩 | 在线伦理电影网 | 国产精品18久久久久网站 | 无码日韩人妻精品久久蜜桃免费 | avav去吧 | 亚洲 欧美 国产 视频二区 | 天天躁日日躁狠狠躁AV麻豆 | 把腿张开再深点好爽宝贝 | 久久亚洲一级α片 | 男女免费观看在线爽爽爽视频 | [高清无码] 波多野结衣| 精品久久久噜噜噜久久7 | 西西人体大胆牲交PP6777 | 轻点灬大ji巴太粗太双性高h | 十8禁用B站在线看漫画 | 精品国产人成亚洲区 | 高hbl双性浪荡古代 高h 大尺度纯肉 np快穿 | 国产WW久久久久久久久久 | 高h乱一受多攻男男 | 不用播放器的黄 | 交换娇妻呻吟声不停中文字幕 | 九九热在线视频精品店 | 70岁妇女牲交色牲片 | 亚洲成人免费观看 | 亚洲国产韩国欧美在线不卡 | 超碰免费视频公开观看 | 伦理片qvod | 能看的黄页最新网站 | 色欲AV久久综合人妻蜜桃 | 开心片色99xxxx| 舔1V1高H糙汉 |