- 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
Showing posts with label scaling complex php websites. Show all posts
Showing posts with label scaling complex php websites. Show all posts
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
Wednesday, February 3, 2010
HipHop for PHP project from Facebook

What is the use?
HipHop can reduce the CPU usage at the server even by 50% depending on the page executing PHP code. Reducing the overhead this way, it can help in scaling complex PHP websites. As per Facebook latest news, HipHop has shown incredible results for Facebook.
How does It optimize?
HipHop doesn't work like a compiler technically, instead HipHop programmatically transforms your PHP source code into highly optimized C++ and then uses g++ to compile it. HipHop executes the source code in a semantically equivalent manner and sacrifices some rarely used features — such as eval() — in exchange for improved performance. HipHop includes a code transformer, a reimplementation of PHP's runtime system, and a rewrite of many common PHP Extensions to take advantage of these performance optimizations.
It's not complete yet, but beta software can be tried before trying it out. I need to still try it out.
Some useful links:
- Twiki : http://github.com/facebook/hiphop-php/wikis
- Dev mailing List : http://groups.google.com/group/hiphop-php-dev
- Source News : http://developers.facebook.com/news.php?blog=1&story=358
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:
Posts (Atom)