Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it meaningful to optimize i++ as ++i to avoid the temporary variable?

Tags:

c++

stl

Someone told me that I can write

for (iterator it = somecontainer.begin(); it != somecontainer.end(); ++it)

instead of

for (iterator it = somecontainer.begin(); it != somecontainer.end(); it++)

...since the latter one has the cost of an extra unused temporary variable. Is this optimization useful for modern compiler? Do I need to consider this optimization when writing code?

like image 266
Thomson Avatar asked Sep 16 '10 07:09

Thomson


People also ask

What is the purpose of temporary variables?

In computer programming, a temporary variable is a variable with short lifetime, usually to hold data that will soon be discarded, or before it can be placed at a more permanent memory location.

Why should you try and avoid using unnecessary variables?

They can be powerful if used properly, but they can also point to a lack of proper design when not used in the correct way. It's a particularly bad practice when variables are used to explain what is going on in the code. It's no different than the issue of comments.

What is correct about the scope of a temporary variable?

the scope of temporary variables is the one in which they have been declared, and all its sub-scopes. Outside its scope of validity, an expression cannot use a variable or an attribute directly. However, attributes can be used in a remote fashion by using a dotted notation on a given agent (see here).

Why is the use of temporary variables encouraged in Python?

It helps prevent statements getting too long and too complex.


4 Answers

It's a good habit to get into, since iterators may be arbitrarily complex. For vector::iterator or int indexes, no, it won't make a difference.

The compiler can never eliminate (elide) the copy because copy elision only eliminates intermediate temporaries, not unused ones. For lightweight objects including most iterators, the compiler can optimize out the code implementing the copy. However, it isn't always obvious when it can't. For example, post-incrementing istream_iterator<string> is guaranteed to copy the last string object read. On a non-reference-counted string implementation, that will allocate, copy, and immediately free memory. The issue is even more likely to apply to heavier, non-iterator classes supporting post-increment.

There is certainly no disadvantage. It seems to have become the predominant style over the past decade or two.

like image 99
Potatoswatter Avatar answered Oct 23 '22 04:10

Potatoswatter


I don't normally consider the prefix ++ an optimization. It's just what I write by default, because it might be faster, it's just as easy to write, and there's no downside.

But I doubt it's worth going back and changing existing code to use the prefix version.

like image 24
jalf Avatar answered Oct 23 '22 04:10

jalf


Yes, that's conceptually the right thing to do. Do you care if it's i++ or ++i? No, you don't. Which one is better? The second one is better since it's potentially faster. So you choose the second (pre-increment).

In typical cases there will be no difference in emitted code, but the iterators can have whatever implementation. You can't control it and you can't control whether the compiler will emit good code. What you can control is how you conveys your intent. You don't need the post-increment here.

like image 27
sharptooth Avatar answered Oct 23 '22 03:10

sharptooth


No. (Stylistically I prefer it, because my native language is English which is mostly a verb-precedes-noun language, and so "increment it" reads more easily than "it increment". But that's style and subjective.)

However, unless you're changing somecontainer's contents in your loop, you might consider grabbing the return value of somecontainer.end() into a temporary variable. What you're doing there will actually call the function on every loop.

like image 43
T.J. Crowder Avatar answered Oct 23 '22 04:10

T.J. Crowder