Is ++i statement in the for loop better than i++?
-
Is ++i statement in the for loop better than i++? I have heard so many people said that. They said it is good for memory management. Could someone gives me a better explanation?
The theory is that with a post increment you have to make a copy of the object, increment the original, and then return the copy of the object. With a pre increment, you increment the object and then return it. Thus pre increment saves the need to make a copy of the object. The good news is that 99.9% of the time, it makes no difference at all. However, the more complicated the objects are (i.e. real objects and not just simple data types), then you can see a performance increase by using pre increment. It all boils down to this: Unless you have a specific need to use post increment, you should use pre increment. It won't buy you anything most of the time, but once you get in the habit of using it, you don't have to worry about the cases where it does buy you something. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Is ++i statement in the for loop better than i++? I have heard so many people said that. They said it is good for memory management. Could someone gives me a better explanation?
-
Is ++i statement in the for loop better than i++? I have heard so many people said that. They said it is good for memory management. Could someone gives me a better explanation?
To answer your original question, there is no difference at all. The compiler will optimize away the copy of
i
that normally gets made in the expressioni++
, because it sees that the copy isn't being used. --Mike-- Eh! Steve! Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me -
To answer your original question, there is no difference at all. The compiler will optimize away the copy of
i
that normally gets made in the expressioni++
, because it sees that the copy isn't being used. --Mike-- Eh! Steve! Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to meOK, but if you don't use optimization (it can be useful for something), it does matter LPCTSTR Dutch = TEXT("Double Dutch :-)");
-
OK, but if you don't use optimization (it can be useful for something), it does matter LPCTSTR Dutch = TEXT("Double Dutch :-)");
Not for a simple loop and/or case such as this.
-
The theory is that with a post increment you have to make a copy of the object, increment the original, and then return the copy of the object. With a pre increment, you increment the object and then return it. Thus pre increment saves the need to make a copy of the object. The good news is that 99.9% of the time, it makes no difference at all. However, the more complicated the objects are (i.e. real objects and not just simple data types), then you can see a performance increase by using pre increment. It all boils down to this: Unless you have a specific need to use post increment, you should use pre increment. It won't buy you anything most of the time, but once you get in the habit of using it, you don't have to worry about the cases where it does buy you something. Tim Smith I'm going to patent thought. I have yet to see any prior art.
[quote]It all boils down to this: Unless you have a specific need to use post increment, you should use pre increment. It won't buy you anything most of the time, but once you get in the habit of using it, you don't have to worry about the cases where it does buy you something.[/quote] Does the above statement applies to all application? or just when you are using for loop? How about in while loop and all other situation? In what situation do we have to use post increment(i++)? Thanks.
-
[quote]It all boils down to this: Unless you have a specific need to use post increment, you should use pre increment. It won't buy you anything most of the time, but once you get in the habit of using it, you don't have to worry about the cases where it does buy you something.[/quote] Does the above statement applies to all application? or just when you are using for loop? How about in while loop and all other situation? In what situation do we have to use post increment(i++)? Thanks.
VW_Red_Jetta wrote: In what situation do we have to use post increment(i++)? None. You don't have to use a post (or pre) increment anywhere, but for convenience a common place is when indexing arrays:
char ch = pStr[offset++];
This is functionally the same as:char ch = pStr[offset]; ++offset;
And results in exactly the same code for simple objects.