PHP : Performance Improvement Tips

  1. Don’t use foreach loop as long as you can avoid it, for loops are faster than any other looping constructs.
    When it comes to various looping constructs and control statements it could differ based on case to case. Please refer http://www.phpbench.com/ by Chris Vincent, he has done remarkable job by providing detailed benchmarks with various combinations. 
  2. echo is faster than print. As echo is one of the language construct which don’t return anything while print will return 0 or 1 based on success or failure. 
  3. include_once is costlier then include statement. As it will have to look whether class definition you are trying to include is already included or not? 
  4. Always use single quotes for long strings and not the double quotes. Because for double quotes php will try to search and evaluate $vars found in that string. So in that case echo ‘It was really a long story to finish ‘. $name is faster then echo “It was really a long story to finish $name”.
    But then echo ‘It was really a long story to finish’ , $name can be faster then first one too, as it don’t required any string manipulation from php and just outputting all arguments to screen. This matters most when we need to do lots of text processing or html to be generate from our application/scripts.
  5. Magic methods like __string,__set,__get can slow down things. But it’s okey as it can give more clarity in our code. Personally I like to use __set and __get as it let me stay away from getter and setter methods. __autoload is expensive and if you don’t really need it then don’t use them. 
  6. Don’t use for($i=0; $i<=count($arrPerson); $i++) {..} instead use,
    for($i=0,$total = count($arrPerson); $i<$total; $i++) {..}. The previous one will call count function for each iteration of the loop while the second one will call count function just once. 
  7. If you can declare a method as static then let it be static, they are faster around 33% then member functions. 
  8. $arrPerson[‘name'] is faster than $arrPerson[name]. So try using single quotes for associative arrays. 
  9. If you can solve your any problem without using regular expressions, then don’t use them. Regex functions are slower than their php counterparts. For example use str_replace instead of preg_replace if it can do serve your purpose. 
  10. If you will provide array as any argument for str_replace it will be faster instead of giving string inputs for single search and replacement.
    Slow:
    str_replace( ‘search’, ‘replace’,$strAssay);
    Fast:
    str_replace(array(‘search’),array(‘replace’),$strAssay);
  11. Try to minimize the relative paths for file include. For relative path includes it will search into default include path then current directory and so on.. So file search in that case can take long time. Instead specify WEB_ROOT constant which will be physical path of your web directory and which could be defined by following way.
    define(’WEB_ROOT’,str_replace(array(’\’),array(’/’),dirname(__FILE__)) . ‘/’);
  12. Identical operator (===) is faster than (==) operator as identical operator will include type checking also. So If( 1 == ‘1′) will return true, If( 0 == ”) will return true while if you use identical operator both this conditions If( 1 === ‘1′) and If(0 === ”) will return false. So it is recommended to use identical operator when you are going to use some boolean variables for deciding the flow/control of your application. 
  13. Don’t use short tags <? and try using <?php, it can heart you when you go an deploy your application on some other servers. 
  14. Don’t use or relay on register_globals or magic quotes and read and configure your php.ini settings carefully. You can read comments over parameters and can set those parameters based on your server setup and application requirement for optimum performance. 
  15. Disable or comment extensions you are not going to use ever from php.ini

In addition to all this develop or follow coding standard for variables, classes, methods and file naming. It will reduce development time due to certainty. Always use proper variable names there is nothing wrong having variable name like $blnIsVARAdminLoggedIn. Proper naming convention will help you and your team members while it comes to maintenance part of the project. Writing comments for not understandable variable or method names would be less helpful. Use editors with auto-complete feature like Eclipse, Aptana which would reduce the hard-work for typing long variable or method names.

Related Material: PHP Coding Standard by Fredrik Kristiansen / DB Medialab

  1. Willia Nordwall 說道:

    This is wonderful! I’m so entertained I just want to read it over and over again. Thank you for making my day a little bit brighter.

  2. I really do not normally annotate concerning weblogs similar to this however in this circumstance plus in keeping along with the comments previously I might take this probability to say just how much I really enjoyed your write-up. Genuinely useful and well written – bless you for sharing this with us!

  3. Ozella Costigan 說道:

    Very good article. I’ve found your site via Yahoo and I’m really happy about the information you provide in your articles. Btw your blogs layout is really broken on the Chrome browser. Would be really great if you could fix that. Anyhow keep up the good work!

  4. left bass guitar 說道:

    Seriously? Don’t get me wrong, I agree with you partially, but when you state something like this you really have to be ready to defend it.

Leave a Reply