PHP function performance
  • 5,261 views,
  • 2013-09-03,
  • 上傳者: Kuann Hung,
  •  0
ff99bd69954e7d2090567a285b0164a5.png
 
PHP 是很好用的 Web 開發語言,可用的函數也相當多,甚至要達成同一個功能的 Function 也很多!這個時候,選用 "效率高" 的函數就是有講究的了!
雖然都差不到 1ms,但是秉持著工程師要求效能的精神,還是有人做了一個網站來幫你計算不同的 function 與不同的使用方式之間的效能差異。如果有興趣的,可以到以下的連結看看不同 function 的效能比較喔!
 
 
文章太少有點騙版面,下面也附上有人建議的 10 個 php 效能提升的小技巧!
 
  1. Use echo instead of print(). As a language construct rather than a function, echo has a slight performance advantage over print().
  2. Echo with commas, not periods. I’m a repeat offender of this one. If you use periods, PHP has to concatenate the string before it outputs. If you use commas, it just outputs them in order with no extra processing.
  3. Avoid function tests in loop conditionals. If you’re looping through an array, for example, count() it beforehand, store the value in a variable, and use that for your test. This way, you avoid needlessly firing the test function with every loop iteration.
  4. Use include() and require() instead of include_once() and require_once(). There’s a lot of work involved in checking to see if a file has already been included. Sometimes it’s necessary, but you should default to include() and require() in most situations.
  5. Use full file paths on include/require statements. Normalizing a relative file path can be expensive; giving PHP the absolute path (or even “./file.inc”) avoids the extra step.
  6. Favor built-in functions over custom functions. Since PHP has to take the extra step of interpreting your custom functions, built-in functions have a performance advantage. More importantly, there are a lot of useful built-in functions that you may never learn about if you always default to writing your own.
  7. Avoid needlessly copying variables. If the variable is quite large, this could result in a lot of extra processing. Use the copy you already whenever possible, even if it doesn’t look pretty (e.g., $_POST['somevariable']).
  8. Pass unchanged variables to a function by reference rather than value. This goes hand-in-hand with the point about needlessly copying variables. Much of the time, your functions only need to use the values from their parameters without changing them. In such cases, you can safely pass those parameters by reference (e.g., function(&$parameter) rather than function($parameter)) and avoid having to make memory-intensive copies.
  9. Debug with error_reporting(E_ALL). Every warning is a performance improvement waiting to happen, but only if you can see it. Cleaning up warnings and errors beforehand can also keep you from using @ error suppression, which is expensive. Just don’t forget to turn off error reporting when you’re done; warnings and errors are expensive as well.
  10. Ditch double quotes for single quotes. There’s some disagreement, but the common wisdom is that PHP has to do extra processing on a string in double quotes to see if it contains any variables. Concatenation with single quotes is marginally faster.
1. 
10 Performance Tips to Speed Up PHP: http://www.wardontheweb.com/10-performance-tips-to-speed-up-php/10 Performance Tips to Speed Up PHP:
Facebook 討論區載入中...
資料夾 :
發表時間 :
2013-09-03 18:42:00
觀看數 :
5,261
發表人 :
Kuann Hung
部門 :
老洪的 IT 學習系統
QR Code :