Hi,
Code:increasing or decreasing for loop?and why?Code:i = i+1 or i++; or i += 1
Thanks
Hi,
Code:increasing or decreasing for loop?and why?Code:i = i+1 or i++; or i += 1
Thanks
A smart compiler might generate the same optimized code for any of those.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
I agree with the proceeding comment about a smart compiler. A dumb compiler, however, might (speculation here) better handle the second version.
On the x86 architecture there is an assembly instruction to increment a register or even a variable in RAM by one.
Might be more easily translated as this:Code:i++;
As opposed to either:Code:inc i
Or this:Code:mov eax, i add eax, 1 mov i, eax
But this is speculation about compilers that aren't quite advanced. Modern compilers should be able to optimize all the expressions you provided.Code:mov eax, i inc eax mov i eax
Otherwise, I would argue in favor of i++ for readability since it's common and an easy shorthand for incrementing a variable by one. Other than that, do whatever you want.
Actually, there's no difference in performance between
andCode:add eax, 1
on a reasonably modern processor (I think 486's may have done INC one cycle faster than a short add). The latter is one byte shorter in number of bytes of code, so if it's part of a larger loop, it could possibly be measued a difference due to code-byte throughput (but this is usually more of an issue if the instructions get really long, like some of the SSE type instructions that are many bytes long).Code:inc eax
Of course, the x86 processor also supports
andCode:add dword ptr i, 1in case the compiler decided to not load the variable i into a register before incrementing/adding to it. Both are shorter than the "load, add, store" variation given by MacGyver - but a "stupid" compiler may well load, add, store simply because it's easier to be generic with that construct and not have to figure out which instruction format is supported by which instruction. Aside from code-size, the time it takes to perform either operation is only marginally different (in favour or the latter code) - however, if the variable "i" is used a lot, then it would of course be much better if the compiler sticks it in a register and not loading/storing in memory all the time.Code:inc dword ptr i
And I would say that any recent compiler will do fairly well on the optimization of such a simple operation. Both GCC and Microsoft compilers for example will definitely "equalize" the three different operations given in the original post - at least under any normal circumstances [it's almost always POSSIBLE to confuse the compiler sufficiently to make it produce pessimal code].
--
Mats
--
Mats
I always use pre-increment unless I actually need to increment after getting the current value, but I'm sure in most cases my compiler would automatically do this for me.
Code:for ( int i = 0; i < max; ++i )
If there's no other use of the variable in a statement, "++i" and "i++" and "i = i + 1" are all equivalent - it is only when you use ++i or i++ as an expression it will make a difference. So yes, the compiler will just use whatever is the "best way to change the value to one higher".
For readability, i++ and ++i are both easier to read (once you are used to C) than "i = i + 1" or similar. I also prefer i += 3 or such, because you don't need to scan both sides to determine that they are equal - not so much of a problem with i += 3, but something like:
compared toCode:p->somefield[index].someother[index2] += 3;
[/code]
p->somefield[index].someother[index2] = p->somefield[index].someother[index2] + 3;
[/code] gets a bit more "interesting" to read.
Making code easy to read helps when it comes to looking at it 6 months after you wrote it.
--
Mats
As cpjust said, I would advise people to get into the habit of always using "++i" over "i++" unless you really want the behaviour defined by "i++". Although, most compilers should be able to optimize around this anyways, "i++" entails more work and memory at the lower level (a copy of the original value of i is first stored before i is incremented), versus "++i". Also, when you get into C++, using "i++" as an iterator over "++i" can significantly decrease the loop's effeciency.
Pre-increment versus Post-increment
Either can be faster, it depends on a whole lot of thing like the starting and continuation expressions, even system architecture. Use whatever makes the most sense.
Which weighs more, a ton of lead or a ton of feathers? That's about the equivalent of what you are asking. Use the one that takes the fewest keystrokes, and save your fingers some unnecessary impact.and why?Code:i = i+1 or i++; or i += 1
Thanks
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"