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. ++i and i++

++i and i++

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
17 Posts 8 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.
  • N Nishad S

    Hi, Is there any advantage in using ++i than i++ in simple loops? For example: for( int i = 0; i < 10; i++) { } for( int i = 0; i < 10; ++i) { } Which is better? Thank you. - NS -

    J Offline
    J Offline
    John M Drescher
    wrote on last edited by
    #4

    Thanks for posting this question... I always use i++ but did not know it could be slower. John

    1 Reply Last reply
    0
    • N Nishad S

      Hi, Is there any advantage in using ++i than i++ in simple loops? For example: for( int i = 0; i < 10; i++) { } for( int i = 0; i < 10; ++i) { } Which is better? Thank you. - NS -

      O Offline
      O Offline
      Owner drawn
      wrote on last edited by
      #5

      Recently I have started using ++i over i++. But you must be careful with assignments. For eg: int j = ++i or i++; You must be fully aware of what you are doing. I use ++i in loops. There is a slight advantage. i++ returns the value and then increments ie j = i; i = i+1; ++i simply increments ie j = (i=i + 1);

      Jesus Loves You and Me :)

      --Owner Drawn --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord

      N 2 Replies Last reply
      0
      • N Nishad S

        Hi, Is there any advantage in using ++i than i++ in simple loops? For example: for( int i = 0; i < 10; i++) { } for( int i = 0; i < 10; ++i) { } Which is better? Thank you. - NS -

        V Offline
        V Offline
        vikas amin
        wrote on last edited by
        #6

        I dont know about integeres but for objects it sure that the performance is better in ++i. U can find the reason at this link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/overl\_12.asp I did a small experiment with integers considering the loop , to test the speed but it is almost same , for bot type of increments :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

        S 1 Reply Last reply
        0
        • G G Haranadh

          Defenetly there is difference. ++i is faster than i++. check with the gettickcount function by calling before this code and after the code. :) Nice talking to you. :-O
          If you judge people, you have no time to love them. -- Mother Teresa

          N Offline
          N Offline
          Nishad S
          wrote on last edited by
          #7

          Thank you. - NS -

          G 1 Reply Last reply
          0
          • S S Senthil Kumar

            There's no advantage for integers like i. But if you're using an STL iterator, for example, there's a definite advantage using ++i over i++. The implementation of the postincrement operator would need to do whatever ++i does, but it would also need to store the original value in a temporary variable and return it, which would be less performant. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            N Offline
            N Offline
            Nishad S
            wrote on last edited by
            #8

            Thank you. - NS -

            1 Reply Last reply
            0
            • O Owner drawn

              Recently I have started using ++i over i++. But you must be careful with assignments. For eg: int j = ++i or i++; You must be fully aware of what you are doing. I use ++i in loops. There is a slight advantage. i++ returns the value and then increments ie j = i; i = i+1; ++i simply increments ie j = (i=i + 1);

              Jesus Loves You and Me :)

              --Owner Drawn --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord

              N Offline
              N Offline
              Nishad S
              wrote on last edited by
              #9

              I was also using i++. Recently changed to ++i... :) Thank you. - NS -

              1 Reply Last reply
              0
              • O Owner drawn

                Recently I have started using ++i over i++. But you must be careful with assignments. For eg: int j = ++i or i++; You must be fully aware of what you are doing. I use ++i in loops. There is a slight advantage. i++ returns the value and then increments ie j = i; i = i+1; ++i simply increments ie j = (i=i + 1);

                Jesus Loves You and Me :)

                --Owner Drawn --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord

                N Offline
                N Offline
                Nishad S
                wrote on last edited by
                #10

                Thank you. - NS -

                1 Reply Last reply
                0
                • N Nishad S

                  Thank you. - NS -

                  G Offline
                  G Offline
                  G Haranadh
                  wrote on last edited by
                  #11

                  YoU ArE MoSt WeLcOmE. ;) Nice talking to you. :-O
                  If you judge people, you have no time to love them. -- Mother Teresa

                  1 Reply Last reply
                  0
                  • G G Haranadh

                    Defenetly there is difference. ++i is faster than i++. check with the gettickcount function by calling before this code and after the code. :) Nice talking to you. :-O
                    If you judge people, you have no time to love them. -- Mother Teresa

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #12

                    G Haranadh wrote:

                    ++i is faster than i++.

                    Could you explain why? It might be a 100th of a ms faster, but other than that, I don't see why ++i would be significantly faster than i++. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    1 Reply Last reply
                    0
                    • N Nishad S

                      Hi, Is there any advantage in using ++i than i++ in simple loops? For example: for( int i = 0; i < 10; i++) { } for( int i = 0; i < 10; ++i) { } Which is better? Thank you. - NS -

                      P Offline
                      P Offline
                      Prakash Nadar
                      wrote on last edited by
                      #13

                      from the VC assembly dump.

                      10: for(i=0;i<5; i++)
                      00401028 mov dword ptr [ebp-4],0
                      0040102F jmp main+2Ah (0040103a)
                      00401031 mov eax,dword ptr [ebp-4]
                      00401034 add eax,1
                      00401037 mov dword ptr [ebp-4],eax
                      0040103A cmp dword ptr [ebp-4],5
                      0040103E jge main+3Fh (0040104f)
                      11: printf("Hello World!\n");
                      00401040 push offset string "Hello World!\n" (0042001c)
                      00401045 call printf (004010b0)
                      0040104A add esp,4
                      0040104D jmp main+21h (00401031)
                      12:
                      13: for(i=0;i<5; ++i)
                      0040104F mov dword ptr [ebp-4],0
                      00401056 jmp main+51h (00401061)
                      00401058 mov ecx,dword ptr [ebp-4]
                      0040105B add ecx,1
                      0040105E mov dword ptr [ebp-4],ecx
                      00401061 cmp dword ptr [ebp-4],5
                      00401065 jge main+66h (00401076)
                      14: printf("Hello World!\n");
                      00401067 push offset string "Hello World!\n" (0042001c)
                      0040106C call printf (004010b0)
                      00401071 add esp,4
                      00401074 jmp main+48h (00401058)

                      so clearly the code for both cases is the same. so no difference if you use the incrementor statements (pre or post) that are independent. -Prakash -Prakash

                      N 1 Reply Last reply
                      0
                      • P Prakash Nadar

                        from the VC assembly dump.

                        10: for(i=0;i<5; i++)
                        00401028 mov dword ptr [ebp-4],0
                        0040102F jmp main+2Ah (0040103a)
                        00401031 mov eax,dword ptr [ebp-4]
                        00401034 add eax,1
                        00401037 mov dword ptr [ebp-4],eax
                        0040103A cmp dword ptr [ebp-4],5
                        0040103E jge main+3Fh (0040104f)
                        11: printf("Hello World!\n");
                        00401040 push offset string "Hello World!\n" (0042001c)
                        00401045 call printf (004010b0)
                        0040104A add esp,4
                        0040104D jmp main+21h (00401031)
                        12:
                        13: for(i=0;i<5; ++i)
                        0040104F mov dword ptr [ebp-4],0
                        00401056 jmp main+51h (00401061)
                        00401058 mov ecx,dword ptr [ebp-4]
                        0040105B add ecx,1
                        0040105E mov dword ptr [ebp-4],ecx
                        00401061 cmp dword ptr [ebp-4],5
                        00401065 jge main+66h (00401076)
                        14: printf("Hello World!\n");
                        00401067 push offset string "Hello World!\n" (0042001c)
                        0040106C call printf (004010b0)
                        00401071 add esp,4
                        00401074 jmp main+48h (00401058)

                        so clearly the code for both cases is the same. so no difference if you use the incrementor statements (pre or post) that are independent. -Prakash -Prakash

                        N Offline
                        N Offline
                        Nishad S
                        wrote on last edited by
                        #14

                        Its amazing... I thought that post increment will cause a temporary instance for keeping the value, and then it will increment. Thank you for revealing a great mistake. - NS -

                        V 1 Reply Last reply
                        0
                        • N Nishad S

                          Its amazing... I thought that post increment will cause a temporary instance for keeping the value, and then it will increment. Thank you for revealing a great mistake. - NS -

                          V Offline
                          V Offline
                          vikas amin
                          wrote on last edited by
                          #15

                          As far as i know it will not effect the integer type but if u are using an c++ object during the increment it will effect preformance Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                          1 Reply Last reply
                          0
                          • N Nishad S

                            Hi, Is there any advantage in using ++i than i++ in simple loops? For example: for( int i = 0; i < 10; i++) { } for( int i = 0; i < 10; ++i) { } Which is better? Thank you. - NS -

                            V Offline
                            V Offline
                            vikas amin
                            wrote on last edited by
                            #16

                            Now i foudnd something that can prove ++i is better then i++. this is in the case ur passing a argument to a function in my case the fun name is add. So ++i i better as it donot create any temp variable to store . As the value is need to be incremented first . But in the late case the value have to be incremented afterwards so a temp variable is needed.. Assmebly code to prove my words U r wrong the case is vice versa Tough to Belive Assembly code to prove my observations 507: Add(++i); 004010CE mov eax,dword ptr [ebp-4] 004010D1 add eax,1 004010D4 mov dword ptr [ebp-4],eax 004010D7 mov ecx,dword ptr [ebp-4] 004010DA push ecx 004010DB call @ILT+15(Add) (00401014) 004010E0 add esp,4 508: Add(i+1); 004010E3 mov edx,dword ptr [ebp-4] 004010E6 add edx,1 004010E9 push edx 004010EA call @ILT+15(Add) (00401014) 004010EF add esp,4 506: Add(1+i); 004010CE mov eax,dword ptr [ebp-4] 004010D1 add eax,1 004010D4 push eax 004010D5 call @ILT+15(Add) (00401014) 004010DA add esp,4 507: Add(i++); 004010DD mov ecx,dword ptr [ebp-4] 004010E0 mov dword ptr [ebp-5Ch],ecx 004010E3 mov edx,dword ptr [ebp-5Ch] 004010E6 push edx 004010E7 mov eax,dword ptr [ebp-4] 004010EA add eax,1 004010ED mov dword ptr [ebp-4],eax 004010F0 call @ILT+15(Add) (00401014) 004010F5 add esp,4 I dont know but how i+1 is more effecient to pass then i++ or ++i Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                            1 Reply Last reply
                            0
                            • V vikas amin

                              I dont know about integeres but for objects it sure that the performance is better in ++i. U can find the reason at this link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/overl\_12.asp I did a small experiment with integers considering the loop , to test the speed but it is almost same , for bot type of increments :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                              S Offline
                              S Offline
                              segment_fault
                              wrote on last edited by
                              #17

                              I'd reiterate the earlier comment that you should BE VERY CAREFUL mixing ++i and i++. It's very easy to skip index 0 in situations where you pre-increment.. only to cause bugs that are sometimes subtle -- especially when dealing with interating through arrays of data. Read more of what I have to say at http://directx9.blogspot.com/

                              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