Sign in to follow this  

C++ for loop performance

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I've read or heard (mostly read) that for(int i=0; i<1000; ++i) has better performance than the classic for(int i=0; i<1000; i++) Can anyone enlighten me? thank you.

Share this post


Link to post
Share on other sites
People like to spread FUD about C++. There will be no difference in speed between the two.

Actually, postfix ++ (as in i++) is usually implemented by making a copy of the value, incrementing the original and returning the copy -- prefix ++ is just incrementing the value. But given that int is a primitive type (i.e., it does not have an overloaded op ++ which would result in a function call) and that you're not using the fact that i++ has different semantics from ++i, the compiler will most likely translate them both to the same code.

As a general rule, don't worry about microoptimizations like that -- they're useless. Optimize your algorithms.

Share this post


Link to post
Share on other sites
++i is called pre-increment. i++ is called post-increment.

Since i is an int here (or really, if it were any POD) there is no real difference. The difference comes in if that is a heavy class.

The reason is because ++i just increments the value and returns the new value. So int i = 5; std::cout << ++i; would output 6.

The other option, i++, must return a copy of i's original value and then increment i. Meaning now there are two ints here instead of just one. So int i = 5; std::cout << i++; would output 5. However after that line i's value is 6. That's because it isn't actually outputting i, it is outputting that copy of i's original value.

Like I said before, for ints it won't matter. But if i was some huge, heavy class and creating a new copy is expensive then you'll notice a difference.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oxyd
Actually, postfix ++ (as in i++) is usually implemented by making a copy of the value, incrementing the original and returning the copy -- prefix ++ is just incrementing the value. But given that int is a primitive type (i.e., it does not have an overloaded op ++ which would result in a function call) and that you're not using the fact that i++ has different semantics from ++i, the compiler will most likely translate them both to the same code.

As a general rule, don't worry about microoptimizations like that -- they're useless. Optimize your algorithms.

That's true, but it's still not a bad habit to use ++i instead of i++ in general, because for more complex types, it can make a difference. And since it doesn't really cost you anything (the code is no harder to read, and takes exactly the same number of characters to type), you might as well use the pre-increment version unless you actually need post-increment semantics.

Share this post


Link to post
Share on other sites
Compiler will generate identical code for post- or pre-increment if functionality is identical. If one version made use of returned value, and another didn't, they wouldn't be identical.

The copy vs. no-copy behavior is relevant only if copy constructor or assignment operator causes side-effects (such as external function call). This may or may not happen with some STL iterators.

Simple rule is:
- Almost always, there will be no difference for functionally identical code
- On rare occasion, pre-increment can be marginally more efficient.

Some developers who make use of STL iterators prefer to use pre-increment consistently to avoid the above rare case.

Share this post


Link to post
Share on other sites
I see no reason to use postincrement in this scenario. What do you gain by writing "i++"? Personally, I have come to like "++i" better, I read it naturally as "increment i".

Share this post


Link to post
Share on other sites
@DevFred I like the look of the post increment syntax more because the majority of operators come after the variable name, so it seems more natural to me. I'm trying to get over it because I use pre-increment much more.

Share this post


Link to post
Share on other sites
Quote:
Original post by ProgramMax
The reason is because ++i just increments the value and returns the new value.
To be exact, ++i reads the value of i, adds one to it, returns the new value, and stores the new value back at some point in time before the next sequence point.

This is why code like a[++i] = i; or even foo(++i,++i) doesn't work: the time when the value is stored back is unknown, so the code is unpredictable.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
a[++i] = i;

There is another reason why that doesn't work: The order of evalution of the = operator is undefined. The compiler might generate code that first looks at the right side to determine the rvalue and then look at the left side to determine the lvalue, or the other way around.

Quote:
Original post by ToohrVyk
foo(++i,++i)

There is another reason why that doesn't work: The order of evalution of function arguments is undefined.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this