# preincrement vs postdecrement question

This topic is 5038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok, this is kinda stupid, but at the same time it's been bothering me for a while. as negligible as it may be in the big picture, isn't ++i faster than i++?

##### Share on other sites

For naitive types, the optimiser will make them both the same.

If the special use of the a++ isn't used, its transformed into ++a.

If it is, then its kept, and it requires a temperary copy (a couple of bytes on average, VERY fast). and the increment, and the destruction of the temp. (also quick)

From,
Nice coder

##### Share on other sites
Depends on the type of i, but in general, ++i will be either faster than or the same speed as i++.

##### Share on other sites
++i is executed directly in the code
i++ waits with incrementing till the entire statement is evaluated.

Its not faster but depends on what you want.

example
int i = 5;int j = ++i;i = 5;int k = i++;

j is 6 and i is 6
k is 5 and i is 6

##### Share on other sites
Quote:
 Original post by Starik1974++i is executed directly in the codei++ waits with incrementing till the entire statement is evaluated.Its not faster but depends on what you want.example*** Source Snippet Removed ***j is 6 and i is 6k is 5 and i is 6

The previous statement is in ERROR. (sorry starik1974.)

correction:

++i is executed with the statement
i++ is executed Between the current statement and the next!

There exactly the same operation, just at different times. (with the statement, and after it).

for eg. printf(i++) is equal to
printf(i)
++i

Same for all the others

Print(i++,j++,k++,l++) =
++i
++j
++k
++l

They operate at exactly the same speed, just with different timing
From,
Nice coder

##### Share on other sites
The speed question arises because of the fact that post-increment has a side effect. For intrinsic types, the compiler can determine if the side effect is used or not and can convert the post-increment into pre-increment. For iterators, however, or user-defined types, it's impossible to tell what other differences may exist.

Choose which one to use in your code based on clear intention, not superstition about performance. If you don't need the side effect of post-increment, then use pre-increment.

##### Share on other sites
WTF??? The pre-increment operator SHOULD be the prefered way to increment user-defined types unless you really do want to modify the original object.

Picture this...for whatever reason your user defined type has dynamic memory associated with it. First of all, allocating memory in a constructor is dangerous, what happens if the allocation fails, ever think about that? Second, the allocation is successfull, but you had to allocate a large chunk of memory and initialize it. As always it depends on the object but usually the initialization isn't cheap but for sake of argument lets say it is. So now you have a temporary object. Well after the current line is finished executing you have to perform a copy and a delete which I guarantee is not going to be as fast as simply incrementing with the pre op.

In conclusion the pre op should be the default mechanism, unless you truly need the data pre-modified.

##### Share on other sites
The previous posters are of course correct. To reiterate...

preincrement (++i) should be preferred, unless you need to use the original value in the statement containing the increment expression, before incrementing it.
It does not always make a diference in speed, particularly when using built-in types, but it does often make a slight difference in speed in other cases.

STL is one place where you really should preincrement whenever possible, for efficiency reasons.

The reasons people sometimes don't do this is that they do not know about the preincrement operator, or do not like the syntax as much.

so a simple for loop should preferably be written like this:
for (int i=0; i<100; ++i)
But if you find yourself having to write ++a by itself after just using 'a' than you may as well combine the two and use a post increment. i.e.
foobar(a);++a;
could be
foobar(a++);
(asuming foobar is not an unsafe macro!) but
b++;
by itself should always be
++b;

##### Share on other sites
Quote:
 STL is one place where you really should preincrement whenever possible, for efficiency reasons.

Or use an algorithm, for even greater efficiency...

Jim.

##### Share on other sites
Or you could just not use either operator. Any operator that constantly generates long threads about it's proper use, is error prone, and is suseptible to undefined behavior just isn't worth the effort. They're also a maintainence nightmare.

In modern compilers you won't realistically get any significant perf increase over sane operators so there's no real reason to use them at all.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 94
• 11
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009632
• ### Who's Online (See full list)

There are no registered users currently online

×