Pre-increment Array For Loops For Speed
PHP March 17th, 2008
I’m familiar with the benefits of removing the array length calculation from your loop by pre-calculating the array length. I’m also familiar the benefits of using indexed arrays instead of associative arrays.
However, I didn’t know that using pre-increment rather than post in your counter calculation is faster.
From blog:
“Furthermore, if we switch from the post-increment operator, to the pre-increment operator, we should also see some time improvements. The difference is that in using the post-increment operator (a++), a copy of the variable is made, the original value is incremented, and then that copy is returned. With pre-increment operator (++a), the value is incremented and then returned, cutting the process down one less step. “
Posts
March 17th, 2008 at 10:45 am
Michael and Cris.. you know whats coming with this post!!