Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is ++i statement in the for loop better than i++?

Is ++i statement in the for loop better than i++?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Link2600
    wrote on last edited by
    #1

    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?

    T V M 3 Replies Last reply
    0
    • L Link2600

      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?

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      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.

      L 1 Reply Last reply
      0
      • L Link2600

        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?

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        I highly recommend ++i over i++. Kuphryn

        1 Reply Last reply
        0
        • L Link2600

          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?

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          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 expression i++, 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

          S 1 Reply Last reply
          0
          • M Michael Dunn

            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 expression i++, 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

            S Offline
            S Offline
            S van Leent
            wrote on last edited by
            #5

            OK, but if you don't use optimization (it can be useful for something), it does matter LPCTSTR Dutch = TEXT("Double Dutch :-)");

            J 1 Reply Last reply
            0
            • S S van Leent

              OK, but if you don't use optimization (it can be useful for something), it does matter LPCTSTR Dutch = TEXT("Double Dutch :-)");

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #6

              Not for a simple loop and/or case such as this.

              1 Reply Last reply
              0
              • T Tim Smith

                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.

                L Offline
                L Offline
                Link2600
                wrote on last edited by
                #7

                [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.

                J 1 Reply Last reply
                0
                • L Link2600

                  [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.

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups