Which Operation is Faster "=" or "+=" ?

This is a discussion on Which Operation is Faster "=" or "+=" ? within the C++ Programming forums, part of the General Programming Boards category; I have two operations. The value of "x" is already set to some value. One of the operations is Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    194

    Which Operation is Faster "=" or "+=" ?

    I have two operations. The value of "x" is already set to some value. One of the operations is
    Code:
    x =  y
    and the other is
    Code:
    x +=  y
    Is one a tiny bit faster than the other? My guess is that the first one is faster, but I'm not certain. Is the speed exactly the same?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Yes, one is likely to be a tiny bit faster than the other.

    Now whether it's always the same answer is much harder to say.

    One thing is for sure, it's hardly ever going to matter to you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    They have a different meaning. Why do you ask?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264

    Red face

    It depends on the definitions of x & y, the code surrounding the usage of them, and the definitions of operator = and operator += for the types of x & y.

    If the answer were ever as simple as a is faster than b (and they do the same thing) then we wouldn't have b.
    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"

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Besides, chances are that your compiler will optimize it for you and you really shouldn't start optimizing before you have a bottleneck in your program. Not to mention that such little changes in your program couldn't make a huge difference.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by thetinman View Post
    I have two operations. The value of "x" is already set to some value. One of the operations is
    Code:
    x =  y
    and the other is
    Code:
    x +=  y
    Is one a tiny bit faster than the other? My guess is that the first one is faster, but I'm not certain. Is the speed exactly the same?
    As others have pointed out, the question does not compare apples and apples. If you meant to compare
    Code:
    x = x + y;
    with
    Code:
    x += y;
    Then there is a difference. In C standardese it is
    A compound assignment of the form E1 op= E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once.
    Whether or not one is faster may or may not be true on a given implementation.

    Of course, all this may be moot in C++ if it is not built-in types.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    as said, they are different in meaning. If you wanna which is faster as you say, you would considering using profiler
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    194
    Quote Originally Posted by Dave_Sinkula View Post
    As others have pointed out, the question does not compare apples and apples. If you meant to compare
    Code:
    x = x + y;
    with
    Code:
    x += y;
    Then there is a difference. In C standardese it isWhether or not one is faster may or may not be true on a given implementation.

    Of course, all this may be moot in C++ if it is not built-in types.
    What's wrong with comparing apples and oranges? I could ask a simple question such as "Which one has more fiber?" or "Which has more vitamin C?"

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    194
    I thought maybe there would be some theoretical answer to this, for example, one operation requires two reads, one add, and one write, and the other operation only requires one read and one write.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    One operation does an assignment. The other does addition and assignment.

    It's just unusual to question which one is faster when they do different things, since normally the point of identifying the faster one is to choose it when both options are possible. Since these two things do different things, there isn't as much reason to identify which is faster.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Assuming x and y are ints... the assembly instructions would be something like this on an x86 processor:

    For x = y;

    Code:
    mov eax, y
    mov x, eax
    For x += y; (or even x = x + y)

    Code:
    mov eax, x
    add eax, y
    mov x, eax

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    147
    Theory can go deeper than that.

    With a simple assignment, and nothing else to go on (context), the optimizer could evaporate the assignment into nothing.

    For example, say the purpose was to preserve the original y and operate upon x as a temporary local value. Depending on context, the compiler might not need to bother.

    Like other posters point out, the type is an all important notion of this context. If x & y are objects, these two operator overloads (at least += must exist), must be consulted to offer any meaningful answer.

    Assuming an atomic primitive, and further assuming that the context of the code is such that the compiler doesn't optimize the assignment into oblivion, the assignment is a simple move, as MacGyver has pointed out.

    In the increment version, while there is another step in the assembler, it may depend on the processor's design & RAM as to whether that actually takes longer in real time.

    It's instructive to try examples like this on various CPU targets and see how there are times when speed is affected as much by ancillary system components that anything we can do it code.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by MacGyver View Post
    Assuming x and y are ints... the assembly instructions would be something like this on an x86 processor:
    Actually, on x86 the CPU can add directly into a memory location, so the x += y code would (or at least could) look like:

    Code:
    mov eax, y
    add x, eax
    I'd wager that the two are identical in speed, at least on x86, if you assume perfect cache behavior.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    And all that is working under the assumption that x and y are not held in registers already.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Either one could be faster; it depends on the types of x and y, the surrounding code, the compiler, and the processor. Looking at those statements as if they will map to particular sequences of machine code instructions is a mistake.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change this simple opeation into power operation..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 12-20-2008, 02:17 PM
  2. Replies: 5
    Last Post: 12-04-2008, 07:15 PM
  3. Which string operation is faster? Which is better?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2008, 11:15 AM
  4. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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