Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
09-02-2011, 04:48 AM #1
Any differences in loop increment method?
The following code results in the exact same output.
Is there an advantage to using i++ over ++i (or visa-versa) in the loop?
Last edited by jmrker; 09-02-2011 at 04:54 AM.
09-02-2011, 05:58 AM #2
And with modern C/C++/Java compilers it makes very very little difference (at most one machine instruction) in *any* circumstance.
The ONLY time it makes a difference is when you *use* the result of the increment (or decrement) in an expression.
Here's why: When you use post-increment, the language specification says "the value of the expression is the value *before* the increment, so the increment must not be visible to the consumer of the expression". (Or words to that effect, depending on what spec your read.)
Whereas with pre-increment, the value is incremented and *then* used in the expression.
The machine code for PRE-increment is quite simple:
loadregister A from i increment A storeregister A into i call push( ) [the register is the argument to the push function]
loadregiste A from i call push() function loadregister A from i increment A storeregister A into i
And back when I was generating code for a C compiler for Z-80 processors (an 8 bit CPU! The biggest register held only16 bits), that's pretty much what we had to do.
But with today's CPUs, their instruction set is so rich that they *probably* can generate code such as this:
/* implemient push[i++] */ load i in A call push() function incrementMemoryLocation i
/* implemient push[++i] */ incrementMemoryLocation i load i in A call push() function
NOTICE THAT NONE OF THIS APPLIES to any of the code that you showed!! Because none of your code "consumed" the value of the incremented variable as part of some larger expression. So in all cases, all the compiler has to do is generate a single increment. And even a not-too-smart compiler will recognize that the two operations are identical in semantics in this case.
[If you are curious: I said that JAVA compilers create an internal tokenized form--the so-called "byte code"--as do all of the MS ".NET" languages. The difference in those languages is that they then have an additional compilation stage--usually called a Just In Time compiler or "JIT" (pronounced just as spelled, with a short "i")--that then renders the byte code into machine code. By writing compilers this way, all the HARD work of smart code generation is the same, done when creating the byte code. And you only have to write a specific "JIT" for each separate CPU architecture.]
Last edited by Old Pedant; 09-02-2011 at 06:01 AM.Be yourself. No one else is as qualified.
09-02-2011, 02:48 PM #3
Thanks for the information.
I had noticed you often used the pre-fixed version of the loop
and I was accustomed to using the other.
I was interested as to why when I saw no difference in the actual effect.
09-02-2011, 08:46 PM #4
Plus because of my compiler experience I learned to use pre unless post is really needed.
The need to do so today is so much less than it was in 1979 (yeah, that's when I wrote the code generator for that C compiler for an 8-bit machine) that even "habit" isn't a good reason. But that's all I have.Be yourself. No one else is as qualified.