++i vs i++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

++i vs i++

Post by Scoody »

I remember someone (MarauderIIC? :)) always doing ++i instead of i++ in for-loop headers, so I thought I'd put it to the test with my newly acquired statistics knowledge ...
So I made this little cruncher to compare them (see attachment).
Here's some results running it on my computer with little or no CPU load

Code: Select all

++i vs. i++
 - the statistical approach
Incrementing...done
Post++
 X: 1.37e-007
 s: 1.17e-005
 ZInterval: [4.76e-008,2.27e-007]

++Pre
 X: 9.16e-008
 s: 9.57e-006
 ZInterval: [1.83e-008,1.65e-007]

ZTest result: ++i != i++
X = Expected time for increment
s = standard deviance
ZInterval is a range in which the time is 95% certain to fall between for the increment.
The ZTest tests if using i++ or ++i really matters, as you can see in my result, ++i wins. (5% chance to give wrong result)

I've noticed that under load the ZTest often gives the answer that using i++ is just as good as ++i, but mostly every run ++i ends up with the smallest amount of time.
Attachments
Postpre.zip
Source for the post vs. pre tester
(2.41 KiB) Downloaded 61 times
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: ++i vs i++

Post by MarauderIIC »

IIRC, i++ makes a copy and increments it and then copies it back, because the value has to be available to use until the end of the statement. ++i modifies the original value. So ++i is always faster, except possibly for basic types such as int where the machine is optimized for such things.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: ++i vs i++

Post by Falco Girgis »

MarauderIIC wrote:IIRC, i++ makes a copy and increments it and then copies it back, because the value has to be available to use until the end of the statement. ++i modifies the original value. So ++i is always faster, except possibly for basic types such as int where the machine is optimized for such things.
Optimized? ++i is going to be faster no matter what optimization. There's still less required to not make a copy than there is to make a copy. :)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: ++i vs i++

Post by MarauderIIC »

I guess I was trying to say that the handling of basic types is so fast that ++i vs i++ wouldn't be noticable. And a smart compiler might notice that basic type's i++ incremented value is not used and change it to ++i, like in a for loop, but it wouldn't necessarily do this with a user-defined type?

Edit: Further, integers and such benefit from value forwarding, assuming I remember my EE classes right, which would mean that the copy wouldn't be used, rather the incremented value is just used straight up. (Right?)

Obviously I'm almost out of my depth :)

Oh and
I remember someone (MarauderIIC? :)) always doing ++i instead of i++ in for-loop headers
Yeah, that's me. :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: ++i vs i++

Post by Scoody »

MarauderIIC wrote:IIRC, i++ makes a copy and increments it and then copies it back, because the value has to be available to use until the end of the statement. ++i modifies the original value. So ++i is always faster, except possibly for basic types such as int where the machine is optimized for such things.
That sounds right, since that's how one would do it when overloading the operator.
MarauderIIC wrote:I guess I was trying to say that the handling of basic types is so fast that ++i vs i++ wouldn't be noticable. And a smart compiler might notice that basic type's i++ incremented value is not used and change it to ++i, like in a for loop, but it wouldn't necessarily do this with a user-defined type?
Well, ++i "won" almost all my runs , but seeing that the time saved is miniscule (as you point out) it doesn't matter which one to use, but it's good for a habit when those user-defined types comes around.
Not that familiar with how the compiler would go around optimizing the increment, but it sounds reasonable, but anyway the numbers won't lie (unless I bugged the calculation...) ;)
LuciDreamTheater

Re: ++i vs i++

Post by LuciDreamTheater »

User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: ++i vs i++

Post by Falco Girgis »

And actually, there is a noticeable difference with integers when you get up into the thousands in loops. It's just a good practice.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: ++i vs i++

Post by K-Bal »

MarauderIIC wrote:IIRC, i++ makes a copy and increments it and then copies it back, because the value has to be available to use until the end of the statement. ++i modifies the original value. So ++i is always faster, except possibly for basic types such as int where the machine is optimized for such things.
Holy shit, I never thought about that :D

Today's lesson learned! ;)
Post Reply