Let me start by saying, I won’t bore you with the list of what I call stupid optimizations.

That’s why I call this 4a, I recommend skipping 4a and go directly to 4b :)

When I first when on my quest to optimize my code, I was disappointed at what I found.
Let me give you a short list of ‘tips’ you find when searching for ‘php optimize code’.

Some have a little value, others are crap.
The results will state things like this:

  • echo vs print
  • pass objects by reference
  • remove comments from your code
  • use single quotes
  • error suppression with @
  • use assosiative arrays
  • use for loops with the count before the loop
  • unset your variables

 

Now, seriously, the echo vs print again??, yes, totally worthless. If I want to make my site faster, there are better ways then to replace print with echo, for the amazing 0.001 second.

 

The pass objects by reference, this one is true, but only IF you know what you’re doing. For base types even if you pass by reference it won’t make a difference. Only use it for objects and even then, you have to watch out, in many cases you’ll actually slow your script down by forcing a copy when it wouldn’t otherwise be made.

 

Removing comments from your code. Well, I had a good laugh with this one. If you’re going to do that, then you might want to install an opcode cacher, see my other blog posts about them. They don’t cache this, so instead, document your code!, as much as possible. More (good)comment makes it readable 5 to 10 years from now, not to mention causing less issues/bugs in the process.

 

Use single quotes. This is true, if you’re interested in that soooo little difference. Personally I made a habit of using the single quotes, but that’s just so that I don’t have to press the shift key to type them(Yea, I’m lazy).

 

Error suppression with @. It is slow, yes, but I never use the @. When writing your code, error handling is part of it. You should not suppress them, you should fix them!. And if errors do happen, then use a try/catch and handle the error, do NOT ignore errors.

 

Use assosiative arrays, I hear that using [5] is 7 times slower then ['me'], haven’t run any benchmarks, but might be a good idea to use this when having big arrays.

 

Use for loops with the count before the loop, when using for($i=0; $i< count(array); $i++) the count(array) is executed every time. It’s useful to put this outside of the for loop. But then again, you could count backwards, $i=count(array);0<=$i; $i–, or something similar.

 

The last one on the list, unset your variables, is only if you use var’s with allot of data in them, or when using big objects.
You won’t win anything if you unset ALL your variables, your script would be spending more time unsetting them then actually keeping them in memory.
This is more the case when doing large queries on a database for example. Unsetting a couple of MB of data might be a good idea :)
Starting php 5.3 there is garbage collection, so that takes care of most of the work for you.

 

So, this post was about optimizing your php code right?, well, instead of doing all those ridicules things listed above, we’ll do it the right way. A profiler!, see where your code is slow, and try and make it faster on those places!.
Optimizing your code that way is WAY more efficient then doing all those small silly things.

 

Don’t get me wrong, if you like coding in those small optimizations, go ahead. I just find it stupid to go and change those things afterwards.

So on to part 4b :) , where I’ll be using XDebug to find the slow spots in the php code.

 

Optimizing your web server: Part 1 – Gzip
Optimizing your web server: Part 2 – Keep Alives
Optimizing your web server: Part 3 – Opcode Caching
Optimizing your web server: Part 4a – PHP
Optimizing your web server: Part 4b – XDebug Profiler
Optimizing your web server: Part 5 – CDN

© 2011 Crazy's Blog Suffusion theme by Sayontan Sinha