|
Posted by Ivαn Sαnchez Ortega on 03/26/07 23:48
mgcclx@gmail.com wrote:
> For loop and while loop. which one is faster?
> I see many articles fighting over it and different people come up with
> different results.
Unless you're designing a compiler, don't worry about that. Worry about the
algorithms instead.
That said, the only possible difference between a for loop and a while loop
is the jump prediction in your architecture's code instruction set. e.g.
the processor can suppose that the condition for a while loop is going to
be true, so it feeds the instructions after the conditional jump (that is,
it's supposing that the jump condition will be successful) and starts
running them in the first stages of the instruction pipeline, saving a
couple of pipeline stages.
Do you have to worry about processor pipelines and jump prediction? Not at
all, unless you're into assembler, or heavy low-level programming. If you
haven't understood a word of the last paragraph, don't worry about loop
speed.
In other words: trust your compiler (or JIT compiler, or interpreter). It
knows about optimizations of conditional jumps better than you (the same
applies to memory caching). Focus on the algorithmics if you want to
shorten processor time.
--
----------------------------------
IvΓ‘n SΓ‘nchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
Give a man a fire and he'll be warm for a day; set him on fire and he'll be
warm the rest of his life.
[Back to original message]
|