Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: (i++) Versus (i = i +1)

Hybrid View

  1. #1

    (i++) Versus (i = i +1)

    Place yer bets.

    Lets say you had a program that consists totally of the statement:
    a++;
    over and over again.

    Then another program that consists of the same number of statements but it's:
    a = a + 1;

    Now the two binaries generated will be the same, but which will take less time to compile?

    I believe a++ is just translated to a = a + 1; in which case it'd have that tiny bit of overhead, but wait, a++; takes up less space, so it can load the file off the hard drive faster!

    No practical value to this, but it's kinda curious.


  2. #2

    Re:(i++) Versus (i = i +1)

    Try it and time it!!

  3. #3
    Advisor Outlaw's Avatar
    Join Date
    May 2001
    Location
    Clifton Park, NY
    Posts
    630

    Re:(i++) Versus (i = i +1)

    I think the compiler sees them as the same thing initially. Take the contents of the register referenced by a and add 1 to it, place that new value back into that register. So same compile time.

  4. #4
    Mentor
    Join Date
    Jul 2001
    Posts
    1,666

    Re:(i++) Versus (i = i +1)

    It would depend on your preprocessor and compiler.
    If you are using a compiler which doesn't support it, then the preprocessor will know and change it.
    However if you are using a newer one which does support it, but the preprocessor doesn't know the compiler supports it, you get the same time.
    Third case, the preprocessor knows that the compiler understands i++ and leaves it alone, then the compiler compiles the binary code.

    Remember whenever you are compiling code a preprocessor goes through and strips out comments and does all sorts of fancy wizadry.

    CP

  5. #5
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    Re:(i++) Versus (i = i +1)

    I believe the compiler will create different codes for the two.
    i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.


  6. #6

    Re:(i++) Versus (i = i +1)

    [quote author=cloverm link=board=9;threadid=9944;start=0#msg90123 date=1099273796]
    I believe the compiler will create different codes for the two.
    i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.


    [/quote]

    Ah but that all depends on whether you're using a RISC or CISC processor. On a RISC processor it's almost guaranteed to compile to the same thing (otherwise it would be a very poor RISC processor). Anyway if I may chip in my theory (assuming RISC), I would think that the lexical scanner would scan through the file (negligible amount of time, but if you really want to break it down, i++ would probably happen faster due to fewer lexical tokens to process), and then the compiler would construct a parse tree for the two of them that probably looks identical in each case, so from there on in it would probably be the same.

  7. #7

    Re:(i++) Versus (i = i +1)

    [quote author=Radar link=board=9;threadid=9944;start=0#msg90121 date=1099271179]
    I think the compiler sees them as the same thing initially. Take the contents of the register referenced by a and add 1 to it, place that new value back into that register. So same compile time.
    [/quote]

    Ah but it takes longer to see i = i +1; because it's a couple of bytes longer.

    The INC versus ADD thing is interesting, and RISC versus CISC. In CISC, it seems INC would be faster than ADD (or else what's the point?), so shouldn't a good compiler optimize i = i + 1; into an INC statement?

    Oh also if the compiler isn't optimizing well ++i versus i++ (i++ has to store the old i, then increment, and return old i)

  8. #8
    Associate
    Join Date
    Aug 2004
    Posts
    10

    Re:(i++) Versus (i = i +1)

    [quote author=cloverm link=board=9;threadid=9944;start=0#msg90123 date=1099273796]
    I believe the compiler will create different codes for the two.
    i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.


    [/quote]That sounds right to me. I'm with you on this one.

  9. #9
    Advisor Lovechild's Avatar
    Join Date
    Apr 2005
    Location
    Århus, Denmark
    Posts
    579

    Re:(i++) Versus (i = i +1)

    [quote author=cloverm link=board=9;threadid=9944;start=0#msg90123 date=1099273796]
    I believe the compiler will create different codes for the two.
    i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.


    [/quote]

    I'm quite sure the compiler will only do that if all optimization is turned off - otherwise it would be logical for the compiler to do the smart thing.

  10. #10
    Associate
    Join Date
    Jun 2005
    Posts
    27

    Re:(i++) Versus (i = i +1)

    I still think it will inc, but i can be wrong, if your using gcc just complie with gcc with -S option to get the assembly output and see for yourselfs.

Similar Threads

  1. X-server versus SLI
    By hoiladon in forum Linux - Software, Applications & Programming
    Replies: 0
    Last Post: 09-18-2007, 09:11 AM
  2. X2 Versus GNOME
    By Ashcrow in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 01-02-2004, 11:32 AM
  3. Handicapping SCO versus Linux
    By mcdougrs in forum General Chat
    Replies: 1
    Last Post: 10-13-2003, 07:33 PM
  4. Win2000 Advanced versus Linux Advanced???
    By Nuray in forum Linux - General Topics
    Replies: 1
    Last Post: 02-27-2003, 09:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •