Friday, August 26, 2011

!(How to Kill PHP Performance)

  1. Wrap sting in single quote instead of double quote As PHP searches variable inside "..." but not in '...'
  2. echo is faster than print print returns valus on success which degrades the performance.
  3. Use echo‘s multiple parameters (or stacked) instead of string concatenation
  4. Use pre-calculations set the maximum value for your for-loops before and not in the loop. ie: for ($x=0; $x < count($array); $x)
  5. Unset or null your variables to free memory, especially large arrays.
  6. Use require() instead of require_once() where possible
  7. Use full paths in includes and requires less time spent on resolving the OS paths.
  8. Close your database connections when you’re done with them.
  9. Avoid use of @ Error suppression with @ is very slow.
  10. Methods in derived classes run faster than ones defined in the base class.
  11. Use pre-increment over post-increment
  12. Use strict code, avoid suppressing errors, notices and warnings thus resulting in cleaner code and less overheads.
  13. Avoid PHP scripts (unless cached) compilation on the fly every time you call them. Install a PHP caching product (such as APC, memcached or eAccelerator or Turck MMCache) to typically increase performance by 25-100% by removing compile times.
  14. Try to use static pages 
    PHP scripts are be served at 2-10 times slower by Apache httpd than a static page. 
  15. else if statements are faster than select statements aka case/switch.
  16. In OOP, declare a method as static, if it can be . Speed improvement is by a factor of 4.
  17. $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.
  18. Use 
    instead of time() or microtime() when script starts
  19. When parsing with XML in PHP try xml2array, which makes use of the PHP XML functions
  20. Use isset where possible in replace of strlen. (ie: if (strlen($foo) < 5) { echo “Foo is too short”; } vs. if (!isset($foo{5})) { echo “Foo is too short”; } ).
  21. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
  22. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
  23. Use ip2long() and long2ip() to store IP addresses as integers instead of strings.
  24. When using header(‘Location: ‘.$url); remember to follow it with a die(); as the script continues to run even though the location has changed or avoid using it all together where possible.
  25. Just declaring a global variable without using it in a function slows things down. PHP checks If it exists or now.
  26. Not everything has to be OOP, often it is just overhead, each method and object call consumes a lot of memory.
  27. Make use of the countless predefined functions of PHP, don’t attempt to build your own as the native ones will be far quicker; if you have very time and resource consuming functions, consider writing them as C extensions or modules.
  28. 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

No comments:


Related Posts with Thumbnails