+25

Which is better in a loop: post-increment (i++) or pre-increment (++i).

Since the post-increment (i++) operator has to return the original value of i, and not the incremented value i + 1, it has to store the old version of i. This means that it typically needlessly uses additional memory to store that value, since, in most cases, we do not actually use the old version of i, and it is simply discarded. While pre-increment (++i), we do not have to save the old value of i — we can simply add to it and return. This aligns much better with the typical use-case in a for-loop, since we rarely need the old value of i in that context. My question is: when making a for-loop is it good practice to use a post-increment (i++) or pre-increment (++i)?

11/8/2019 1:37:36 PM

Meny Evolving

41 Answers

New Answer

+17

Surprisingly ++i is better than i++ since it's faster on the cpu. Here's the explanation: i++ does{ temp = i; i=i+1; return temp; } while ++i only does { i = i+1; return i; }

+13

Aymane Boukrouh [Inactive] I often use the post-increment, which is also the most commonly use. I want opinion of other programmers as to which method is better so as to adopt.

+12

I always write ++i, except when I specifically need the trait of i++ to change the value after the statement. From what I've heard, compiler optimizes i++ away anyway when possible, but I haven't really looked into compiled code.

+9

ABADA S Parallel processing (more cores) and instruction pipelining can make n++ faster as it is sort of non blocking create a temporary add 1 and reassign. ++i is sort of blocking as it increments inplace, till first inplace operation is complete, you cannot add again. The difference is very minor. And this may not be true all the time (i.e why i said can). If you benchmark you are going to get different results, sometimes ++i win sometime i++ wins. Modern C/C++ compilers are incredibly complex ! And sorry i do not have indepth explaination.

+7

Both loop are good

+7

The thing is that, you should be able to use them where they ought to be.... If you solely need to increment, you can use any of them. (i.e., pre/post)

+7

https://www.sololearn.com/post/171457/?ref=app

+7

Neither are best, you use the most appropriate one according to the needs of your code. ☺

+7

Isn't it somewhat strange that the most overasked questions are also the most overanswered ones?

+7

Meny Evolving Working with primitives, i++ or ++i hardlly matters. Theoritically ++i should be faster as it doesn't involve creating a temporary but modern compilers can make i++ faster than ++i. Working with itetators, prefer prefix version over postfix as creation of temporary can be expensive with objects.

+5

~ swim ~ what is the difference in modern pc to execute i++ faster?

+5

thanks ~ swim ~

+4

Meny Evolving I agree

+4

Post increment(i++) is better in case where we want i value increased in next step not in current step where as pre increment is good when we required instant increment in value of i that is in current step only. int i=2; Cout<<++i; //prints 3 Cout<<i++; //prints 2 Cout<<i;. //prints 3 This Is what is meant.

+4

++i

+4

Both are good, the choice is up to you.

+4

There is no difference if you are not using the value after increment in the loop. ... The difference is in the return value. The return value of "++i" will be the value after incrementing i. The return of "i++" will be the value before incrementing

+4

HonFu Completely agree with you, it's after all just i++ and ++i.

+3

++i

+3

I personally like prefix