Thoughts?
Edited by MMDE, 16 November 2012 - 09:58 PM.
Posted 16 November 2012 - 09:57 PM
Edited by MMDE, 16 November 2012 - 09:58 PM.

Posted 16 November 2012 - 10:43 PM
Posted 16 November 2012 - 11:52 PM
Posted 16 November 2012 - 11:53 PM
Posted 17 November 2012 - 12:45 PM
Posted 17 November 2012 - 01:40 PM
reset () and each (), it won't do exactly the same as the foreach () loop under the hood. The effect is the same, make no mistake about that, but since you're calling additional PHP functions the PHP parser has to do more work.foreach () explanation you refer to the variable being referenced, even though you haven't passed it by reference at all. It also seems that this (apparent) confusion has lead you to mix the warning from the PHP page into this all, at least from what I could understand. It's a bit unclear what you meant, so I might be mistaken on that bit.count () for every iteration. O(n) isn't always the same as O(n), when comparing two different functions. The increase in complexity might very well be the same for both, but it all depends upon how many instructions per iteration they are running which is the faster (not to mention the size of N).foreach () faster than the alternatives (by a very small margin), but in the right circumstances a O(n) operation can even be faster than a O(log n) operation. All the notation tells you, after all, is how the performance is influenced by an increase in the dataset to be processed.array_flip () and isset () can actually be a lot faster than the alternatives. Especially with big arrays.
Edited by Christian F., 17 November 2012 - 01:42 PM.
Posted 17 November 2012 - 02:07 PM
The reason that one's (slightly) faster than the other, is that they don't preform the exact same machine code instructions. Even if you construct a while loop with
reset ()andeach (), it won't do exactly the same as theforeach ()loop under the hood. The effect is the same, make no mistake about that, but since you're calling additional PHP functions the PHP parser has to do more work.
When it comes to your article I'm afraid it's a bit inaccurate. In the
foreach ()explanation you refer to the variable being referenced, even though you haven't passed it by reference at all.
The two alternatives you've posted are also woefully inefficient in comparison to the first, since you're calling
count ()for every iteration.
O(n) isn't always the same as O(n), when comparing two different functions. The increase in complexity might very well be the same for both, but it all depends upon how many instructions per iteration they are running which is the faster (not to mention the size of N).
So not only is
foreach ()faster than the alternatives (by a very small margin), but in the right circumstances a O(n) operation can even be faster than a O(log n) operation. All the notation tells you, after all, is how the performance is influenced by an increase in the dataset to be processed.
Posted 17 November 2012 - 02:19 PM
0 members, 0 guests, 0 anonymous users