19-year old Chris Vincent from down there Brisbane, Australia put together a really interesting programming tool aka cheat sheet aka PHPBench.com, by comparing the different approaches we usually adopt while coding in PHP.
Apart from the extremely useful results showcased there, the whole page is a piece of simplicity sex, with to-the-point typography and clear colour cues for everything.
Since I don’t consider myself a fluent PHP programmer, imagine my shock when I saw that the while(list => each) loop is *so* much faster than my beloved foreach in modifying loops.
Foreach, you’re out!
P.S. I guess you can take them all results with a grain of salt, but they seem pretty much well worked on and accurate. Thoughts?











7 comments on this post
Vangelis #1
06.Jun.08
That’s old news but nobody cares. By the time you optimize a web application you can get a 2x faster server for the same money. So… Speed of coding not speed of code. Foreach rocks
Sugar #2
06.Jun.08
@Vangelis
Also noticed that pre-calculated array lengths, a long-standing optimization for loops, actually have no positive effect in PHP efficiency. Quite a shock too.
I’m right with you on the quick coding bit, but experience has proven us wrong – when coding in a hurry, you’ll pay for it later.
Twitter, anyone?
stelabouras #3
06.Jun.08
Indeed, as they say:
“Programming is like sex, one mistake and you have to support it for the rest of your life.”(Michael Sinz)
nikan #4
06.Jun.08
You confused me. The conclusion at the phpbench.com is quite the opposite from yours:
“In all cases I’ve found that the foreach loop is substantially faster than both the while() and for() loop procedures. One thing to note is that when using an entire loop from the start it’s extremely good to use the reset() function in all examples”
What am I (or you) misinterpreting?
Sugar #5
06.Jun.08
@nikan
That’s for the Read Loop, when you just want to traverse an array and do some stuff with its values. Check the Modify Loop, that’s what I meant.
Peter #6
06.Jun.08
the ‘foreach’ results are now the fastest.
Sugar #7
06.Jun.08
@Peter Uhm, I still see ‘foreach’ giving a +632% overhead in Modify Loops. What did I lose?