Which is more optimized?

This is a discussion on Which is more optimized? within the C Programming forums, part of the General Programming Boards category; Hi, Code: increasing or decreasing for loop? Code: i = i+1 or i++; or i += 1 and why? Thanks...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Which is more optimized?

    Hi,

    Code:
    increasing or decreasing for loop?
    Code:
    i = i+1 
    or
    i++;
    or
    i += 1
    and why?

    Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    A smart compiler might generate the same optimized code for any of those.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I agree with the proceeding comment about a smart compiler. A dumb compiler, however, might (speculation here) better handle the second version.

    On the x86 architecture there is an assembly instruction to increment a register or even a variable in RAM by one.

    Code:
    i++;
    Might be more easily translated as this:

    Code:
    inc i
    As opposed to either:

    Code:
    mov eax, i
    add eax, 1
    mov i, eax
    Or this:

    Code:
    mov eax, i
    inc eax
    mov i eax
    But this is speculation about compilers that aren't quite advanced. Modern compilers should be able to optimize all the expressions you provided.

    Otherwise, I would argue in favor of i++ for readability since it's common and an easy shorthand for incrementing a variable by one. Other than that, do whatever you want.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    I agree with the proceeding comment about a smart compiler. A dumb compiler, however, might (speculation here) better handle the second version.

    On the x86 architecture there is an assembly instruction to increment a register or even a variable in RAM by one.

    Code:
    i++;
    Might be more easily translated as this:

    Code:
    inc i
    As opposed to either:

    Code:
    mov eax, i
    add eax, 1
    mov i, eax
    Or this:

    Code:
    mov eax, i
    inc eax
    mov i eax
    But this is speculation about compilers that aren't quite advanced. Modern compilers should be able to optimize all the expressions you provided.

    Otherwise, I would argue in favor of i++ for readability since it's common and an easy shorthand for incrementing a variable by one. Other than that, do whatever you want.
    Actually, there's no difference in performance between
    Code:
    add eax, 1
    and
    Code:
    inc eax
    on a reasonably modern processor (I think 486's may have done INC one cycle faster than a short add). The latter is one byte shorter in number of bytes of code, so if it's part of a larger loop, it could possibly be measued a difference due to code-byte throughput (but this is usually more of an issue if the instructions get really long, like some of the SSE type instructions that are many bytes long).

    Of course, the x86 processor also supports
    Code:
    add dword ptr i, 1
    and
    Code:
    inc dword ptr i
    in case the compiler decided to not load the variable i into a register before incrementing/adding to it. Both are shorter than the "load, add, store" variation given by MacGyver - but a "stupid" compiler may well load, add, store simply because it's easier to be generic with that construct and not have to figure out which instruction format is supported by which instruction. Aside from code-size, the time it takes to perform either operation is only marginally different (in favour or the latter code) - however, if the variable "i" is used a lot, then it would of course be much better if the compiler sticks it in a register and not loading/storing in memory all the time.

    And I would say that any recent compiler will do fairly well on the optimization of such a simple operation. Both GCC and Microsoft compilers for example will definitely "equalize" the three different operations given in the original post - at least under any normal circumstances [it's almost always POSSIBLE to confuse the compiler sufficiently to make it produce pessimal code].

    --
    Mats

    --
    Mats

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I always use pre-increment unless I actually need to increment after getting the current value, but I'm sure in most cases my compiler would automatically do this for me.
    Code:
    for ( int i = 0; i < max; ++i )

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    I always use pre-increment unless I actually need to increment after getting the current value, but I'm sure in most cases my compiler would automatically do this for me.
    Code:
    for ( int i = 0; i < max; ++i )

    If there's no other use of the variable in a statement, "++i" and "i++" and "i = i + 1" are all equivalent - it is only when you use ++i or i++ as an expression it will make a difference. So yes, the compiler will just use whatever is the "best way to change the value to one higher".

    For readability, i++ and ++i are both easier to read (once you are used to C) than "i = i + 1" or similar. I also prefer i += 3 or such, because you don't need to scan both sides to determine that they are equal - not so much of a problem with i += 3, but something like:
    Code:
        p->somefield[index].someother[index2] += 3;
    compared to
    [/code]
    p->somefield[index].someother[index2] = p->somefield[index].someother[index2] + 3;
    [/code] gets a bit more "interesting" to read.

    Making code easy to read helps when it comes to looking at it 6 months after you wrote it.

    --
    Mats

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    As cpjust said, I would advise people to get into the habit of always using "++i" over "i++" unless you really want the behaviour defined by "i++". Although, most compilers should be able to optimize around this anyways, "i++" entails more work and memory at the lower level (a copy of the original value of i is first stored before i is incremented), versus "++i". Also, when you get into C++, using "i++" as an iterator over "++i" can significantly decrease the loop's effeciency.
    Pre-increment versus Post-increment

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by Moony View Post
    Hi,

    Code:
    increasing or decreasing for loop?
    Either can be faster, it depends on a whole lot of thing like the starting and continuation expressions, even system architecture. Use whatever makes the most sense.
    Code:
    i = i+1 
    or
    i++;
    or
    i += 1
    and why?

    Thanks
    Which weighs more, a ton of lead or a ton of feathers? That's about the equivalent of what you are asking. Use the one that takes the fewest keystrokes, and save your fingers some unnecessary impact.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimized inproduct of floats/doubles? (sse?)
    By Phuzzillogic in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2009, 09:14 AM
  2. Library Code Optimized?
    By MacNilly in forum C Programming
    Replies: 4
    Last Post: 11-06-2007, 04:27 AM
  3. Problem in the optimized / release build
    By g4j31a5 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2006, 06:32 AM
  4. Why isn't this C program optimized?
    By hzmonte in forum C Programming
    Replies: 19
    Last Post: 09-08-2006, 06:42 AM
  5. Compiler Optimized code
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-18-2004, 01:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21