++ i or i ++ in for loops ??

Possible duplicate:
Is there any performance difference between i ++ and i ++ in C ++?

Is there a reason some programmers write ++i in a normal loop rather than write i++ ?

+44
c ++ for-loop post-increment pre-increment
Nov 23 '10 at 22:37
source share
9 answers

For integers, there is no difference between pre-and post-increment.

If i is an object of a non-trivial class, then ++i usually preferable because the object is modified and then evaluated, while i++ changed after evaluation, so a copy is required.

+62
Nov 23 '10 at 22:40
source share

++i bit more efficient due to its semantics:

 ++i; // Fetch i, increment it, and return it i++; // Fetch i, copy it, increment i, return copy 

For indices of type int, the gain of efficiency is minimal (if any). For iterators and other objects with a heavier weight, avoiding this copy can be a real gain (especially if the loop body does not contain a lot of work).

As an example, consider the following cycle, using the theoretical BigInteger class, which provides arbitrary precision integers (and, therefore, some vector objects):

 std::vector<BigInteger> vec; for (BigInteger i = 0; i < 99999999L; i++) { vec.push_back(i); } 

This i ++ operation involves building a copy (i.e. a new operator, a digital copy) and destroying (deleting the operator) for a loop that will do nothing but essentially make another copy of the index object. Essentially, you doubled the work you are doing (and most likely increased memory fragmentation) by simply using a postfix increment where the prefix would be sufficient.

+78
Nov 23 '10 at 22:41
source share

++i is a pre-increment; i++ is an increment.
The disadvantage of post-increment is that it generates an extra value; it returns a copy of the old value when i changes. Therefore, you should avoid it whenever possible.

+6
Nov 23 '10 at 22:40
source share

With integers, this is preference.

If the loop variable is a class / object, this can make a difference (only profiling can tell you if it makes a significant difference), since the version followed by incrementing requires you to create a copy of that object that is discarded.

If making this copy is an expensive operation, you pay this expense once every time you go through the cycle, for no reason.

If you are used to always using ++i for loops, you don’t need to stop and think about whether what you are doing in this particular situation makes sense. You just always.

+4
Nov 23 '10 at 22:44
source share

There is a reason for this: performance. I ++ generates a copy, and this is waste if you drop it right away. Of course, the compiler can optimize this copy if i is primitive, but cannot if it is not. See this question.

+2
Nov 23 '10 at 22:41
source share

No compiler that has its weight in salt will work differently between

 for(int i=0; i<10; i++) 

and

 for(int i=0;i<10;++i) 

++ me and i ++ have the same cost . The only difference is that the return value of ++ i is i + 1, while the return value of i ++ is i.

So, for those who prefer ++ i, there is probably no valid justification, but only personal preference.

EDIT: This is wrong for classes, as every other post says. I ++ will generate a copy if I am a class.

+1
Nov 23 '10 at 22:39
source share

As noted earlier, pre-increment is usually faster than post-increment for user types. To understand why this is so, look at a typical code template for implementing both operators:

 Foo& operator++() { some_member.increase(); return *this; } Foo operator++(int dummy_parameter_indicating_postfix) { Foo copy(*this); ++(*this); return copy; } 

As you can see, the prefix version just modifies the object and returns it by reference.

The postfix version, on the other hand, must make a copy before the actual increment, and then this copy will be copied back to the caller by value. From the source code, it is obvious that the postfix version should work more, because it includes a call to the prefix version: ++(*this);

For built-in types, this does not make any difference until you discard that value, that is, until you insert ++i or i++ into a larger expression such as a = ++i or b = i++ .

+1
Nov 23 '10 at 22:51
source share

Personal preferences.

Usually. Sometimes it matters, but not to seem like a jerk here, but if you have to ask, it probably doesn't.

0
Nov 23 2018-10-23T00:
source share

when you use postfix, it creates an instance for more objects in memory. Some say it's better to use a suffix operator for a loop

0
Nov 23 '10 at 22:40
source share



All Articles