- Wrap sting in single quote instead of double quote As PHP searches variable inside "..." but not in '...'
- echo is faster than print print returns valus on success which degrades the performance.
- Use echo‘s multiple parameters (or stacked) instead of string concatenation
- 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)
- Unset or null your variables to free memory, especially large arrays.
- Use require() instead of require_once() where possible
- Use full paths in includes and requires less time spent on resolving the OS paths.
- Close your database connections when you’re done with them.
- Avoid use of @ Error suppression with @ is very slow.
- Methods in derived classes run faster than ones defined in the base class.
- Use pre-increment over post-increment
- Use strict code, avoid suppressing errors, notices and warnings thus resulting in cleaner code and less overheads.
- 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.
- Try to use static pagesPHP scripts are be served at 2-10 times slower by Apache httpd than a static page.
- else if statements are faster than select statements aka case/switch.
- In OOP, declare a method as static, if it can be . Speed improvement is by a factor of 4.
- $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.
- Use$_SERVER[’REQUEST_TIME’] instead of time() or microtime() when script starts
- When parsing with XML in PHP try xml2array, which makes use of the PHP XML functions
- 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”; } ).
- Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.
- Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
- Use ip2long() and long2ip() to store IP addresses as integers instead of strings.
- 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.
- Just declaring a global variable without using it in a function slows things down. PHP checks If it exists or now.
- Not everything has to be OOP, often it is just overhead, each method and object call consumes a lot of memory.
- 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.
- 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
Friday, August 26, 2011
!(How to Kill PHP Performance)
All Blogs :
http://timir126.blogspot.com
http://vishnu-agarwal.blogspot.com
http://jobkeeper.blogspot.com
http://gethouse.blogspot.com
http://vishnu-lamp.blogspot.com
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment