which increment is usefull

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjuks
    New Member
    • Dec 2007
    • 72

    which increment is usefull

    Hi All,

    Is any one tell me the difference between i = i + 1 and i++.
    Which one do you prefer to use?

    Thanks,
    Manjunath
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    They both do the same thing. Personally I prefer the i++ option - much tidier.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Also in C++ when working with objects rather than integers i++ is rather more efficient than i = i + 1 and in fact ++i is even more efficient.

      Comment

      • vipin sharma
        New Member
        • Jul 2009
        • 16

        #4
        After studying little bit of assembly programming in microprocessor I feel that i++ and i+1 differs in the way they are computed at register level.

        i++ requires just one register of cpu which stores value of i, and it increments that register.
        its assembly code: INC I;

        but in case of i+1 it requires two registers one to store i and other to store 1.
        its assembly code: ADD I,1;

        As far as speed is concerned i dont think they differ at microprocessor level because both operations takes one clock cycle.

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          On modern x86 (e.g. netburst ) INC may be slower than ADD - because it doesn't modify carry flag, and if the next op is some ( not any ) conditional branch, cpu will flush the pipeline (so called partial flags stall)

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Actually any conclusions you draw about C++ language structure based on the assembly (machine code) that you end up with apply only for the specific platform/compiler combination.

            Any other compiler may produce different machine code (especially if it has a different optimiser) and any other platform (processor) will have a different instruction set with different characteristics .

            Comment

            • unauthorized
              New Member
              • May 2009
              • 81

              #7
              If the performance difference between ++i and i++ matters for a project, I am guessing that anyone even remotely competent is going to run a few tests to measure it. It's true that ++i tends to generally be faster on x86 based systems, but you can't really assume that unless you test it on every single x86 CPU out there.

              With that said, using either operators will have no notable impact unless all of this is true:
              1) 99% or more CPU usage. Not 60%, 80% or 90%.
              2) increment is done in a performance critical section multiple times
              3) increment cannot be optimized or the compiler will not optimize it

              Comment

              • manjuks
                New Member
                • Dec 2007
                • 72

                #8
                Hi All,

                Thanks for your need full replies.

                Thanks,
                Manjunath

                Comment

                Working...