PDA

Click to See Complete Forum and Search --> : foreach fails. faster iterations?


tomcatexodus
Aug 25th, 2010, 01:34 AM
For discussion;

I've read up on (and performed my own) benchmarking for lengthy tasks and their faster functional equivalencies in PHP. Often it's said that micro-optimizing code is a worthless task, but I figure; sort them out before deployment to minimize code maintenance if the project grows.

A common one is foreach vs. various. foreach often fails in efficiency. Now, in my tests (and I've not seen this method much) when you want to iterate straight through an array, the following works the fastest:

$i_ceil = count($my_array);
while($i++ <= $i_ceil){
//process on $my_array[$i]
}

Has anyone encountered faster? Has anyone found this approach unfeasible?

Zach_VB6
Aug 25th, 2010, 03:39 AM
Check out: http://www.phpbench.com/

tomcatexodus
Aug 25th, 2010, 11:57 AM
Oh I have already, thank you Zach_VB6. In fact, I've considered creating my own site for that purpose, complete user submissions, discussion, and multiple interpreted language categories.

I actually found in my testing (on many runs) that the while(){} iterator is several hundred times faster than foreach and it's equally slow counterparts. I'll post my brief analysis later this afternoon.

squared
Sep 13th, 2010, 05:54 PM
I wonder if ++$i < $i_ceil is faster than $i++ <= i_ceil?

tomcatexodus
Sep 21st, 2010, 04:27 PM
I wonder if ++$i < $i_ceil is faster than $i++ <= i_ceil?

Haven't tested, wouldn't think there would be a difference though. They're identical except in timing.