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

  1. #16
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by MacGyver View Post
    For x += y; (or even x = x + y)
    Code:
    mov eax, x
    add eax, y
    mov x, eax
    Actually I think it would be more like:
    for x += y;
    Code:
    mov eax , y
    add x , eax
    Last edited by abachler; 05-31-2007 at 02:30 PM.

  2. #17
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    What happens when you ask a board of programmers such a question. Besides, who said anything about an x86 processor. I've done more assembly elsewhere. (Usually because I couldn't use C, but hey. )

    Quote Originally Posted by abachler View Post
    Actually I think it would be more like:
    for x += y;
    Code:
    mov eax , y
    add x , eax
    Seems brewbuck thinks the same thing. Déjà vu.
    Quote Originally Posted by brewbuck, something like 2 hours earlier ;) View Post
    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.
    My x86 instruction sheet doesn't have speed listed... could not two processors differ in the # of cycles needed? (Between AMD & Intel?)

    Edit: The multiquote button works across pages, very impressive.

    Edit: According to this page, the clock cycles, on a 486 (best it had...) I believe it works out to:
    Code:
    mov eax, x  ; 1
    add eax, y  ; 2
    mov x, eax  ; 1
                ; Total = 4
    
    mov eax, y  ; 1
    add x, eax  ; 3
                ; Total = 4
    Last edited by Cactus_Hugger; 05-31-2007 at 03:11 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #18
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    well,
    Code:
    mov eax , y
    add x , eax
    would take 2 clock cycles (pentium era cpus execute most ops in 1 cycle), since the add is data dependant on the mov, so the pipline stalls at least a little bit. however, since it would take fewer memory locations, it woudl reduce cache useage, and so be faster by potentially reducing cache delays.

    however, if you intend to use x in the next operation thusly -
    Code:
    x += y;
    x += z;
    x += w;
    you could optimize it like so :
    Code:
    mov eax , x
    add eax , y 
    add eax , z 
    add eax , w
    mov x , eax
    Last edited by abachler; 05-31-2007 at 03:20 PM.

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cactus_Hugger View Post
    My x86 instruction sheet doesn't have speed listed... could not two processors differ in the # of cycles needed? (Between AMD & Intel?)
    Nah. Simple integer moves and additions have always taken only a single cycle, again, assuming perfect cache behavior.

  5. #20
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Most sheets dont list the opcode timings anymore because they wouldnt be accurate in any case. The timing of an instruction on a pipelined cpu is too dependant on the instructions before and after it for such a figure to be meaningful. what with out-of-order execution, branch prediction, tentative execution, it becomes pointless.

    Code:
    mov eax , x
    add eax , ebx
    mov ecx , y
    add edx , ebx
    takes 3 cycles
    Code:
    add edx , ebx
    mov eax , x
    add eax , ebx
    mov ecx , y
    takes 2 cycles

    both sequences produce the same result and execute the same instructions, only the order is different
    Last edited by abachler; 05-31-2007 at 04:48 PM.

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by thetinman View Post
    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?"
    Because any answer is specious at best, even comparing equivalent operations. With regard to the language, "Which is faster?" questions have no merit.

    With regard to a particular implementation, on a particular OS, with particular options, at a given moment in time is what you are left with. But you'd be surprised by "silly" advice from the past that was once "common knowledge", and any result of this query is likely to be viewed as "silly" in the future.
    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. #22
    Registered User
    Join Date
    May 2007
    Posts
    5

    Thumbs down Both are different

    Both have the diffrent meanings
    while x=y; is assigning the value of y to x and x+=y; transforms to x=x+y;
    both have no relation whatsoever.


    May i ask what was the purpose of asking this question?
    Did you mean to ask which one is faster between these two?
    x=x+y;
    x+=y;


    -----------------
    http://learning-computer-programming.blogspot.com

  8. #23
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by Dave_Sinkula View Post
    Because any answer is specious at best, even comparing equivalent operations. With regard to the language, "Which is faster?" questions have no merit.

    With regard to a particular implementation, on a particular OS, with particular options, at a given moment in time is what you are left with. But you'd be surprised by "silly" advice from the past that was once "common knowledge", and any result of this query is likely to be viewed as "silly" in the future.
    Are you sure? Wouldn't
    Code:
    y = powf(x,2);
    always be slower than
    Code:
    y = x*x;
    on all platforms, OSs, and in all languages? I suppose with optimation, or certain architectures, they could be the same speed, but the former will never be faster than the later. I could therefore conclude that, if speed is an issue, I should definitely use the later.
    Last edited by thetinman; 06-05-2007 at 07:35 AM. Reason: Add stuff

  9. #24
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by Daved View Post
    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.
    This question looks strange to many people so I propose an example. Here's one function
    Code:
    double *myFunction(double a, double b, double c){
       double z;
       z = a*b + c
       return z;
    }
    which is called as
    [code]
    z = myFunction(a,b,c);
    [code]

    Code:
    double *myFunction(double a, double b, double c){
       double z;
       z = b*c + a;
       return z;
    }
    which is call as
    [code]
    z += myFunction(a,b,c);
    [code]

    The problem itself is set up differently for each function. While the input parameters are different, the answer z, is the same. Speaking in terms of efficiency of code, the only difference between the two implementations is that one uses z = myFunction(a,b,c), while the other uses z += myFunction(a,b,c).

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But that's not really a question in regard to the language.

    But had you used pow(x, 2), I know some systems where this will probably really optimize down to x*x.
    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

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by thetinman View Post
    This question looks strange to many people so I propose an example. Here's one function
    Code:
    double *myFunction(double a, double b, double c){
       double z;
       z = a*b + c
       return z;
    }
    Error: cannot implicitly convert from double* to double.

    which is called as
    [code]
    z = myFunction(a,b,c);
    [code]

    Code:
    double *myFunction(double a, double b, double c){
       double z;
       z = b*c + a;
       return z;
    }
    The two implementations are identical.

    which is call as
    [code]
    z += myFunction(a,b,c);
    [code]
    The two calls are identical too. What is done with the return value (= or +=) is of no concern to the function.

    The problem itself is set up differently for each function. While the input parameters are different,
    You pass the same parameters. At least they have the same names.

    the answer z, is the same. Speaking in terms of efficiency of code, the only difference between the two implementations is that one uses z = myFunction(a,b,c), while the other uses z += myFunction(a,b,c).
    No, that is not a difference of the implementations. That's a difference of what you're doing with the return value.

    I'm afraid you're not making any sense.
    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

  12. #27
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by CornedBee View Post
    Error: cannot implicitly convert from double* to double.


    The two implementations are identical.


    The two calls are identical too. What is done with the return value (= or +=) is of no concern to the function.


    You pass the same parameters. At least they have the same names.


    No, that is not a difference of the implementations. That's a difference of what you're doing with the return value.

    I'm afraid you're not making any sense.
    Sorry that should be
    Code:
    double *myFunction(double a, double b, double c){
       double *z;
       z = a*b + c
       return z;
    }
    and
    Code:
    double *myFunction(double d, double e, double f){
       double *z;
       z = d*e+f
       return z;
    }
    I'm simply trying to propose a way that y = x and y += x could yield the same value.
    Last edited by thetinman; 06-05-2007 at 08:08 AM. Reason: make more literal

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The functions are still the same, just with different parameter names, and will still not compile: now you're trying to assign the result of a*b+c, which is a double, to a pointer to double.

    And the only way y=x and y+=x yield the same value is if y is 0. The right side, where your function is, has nothing to do with it.
    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

  14. #29
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by CornedBee View Post
    But that's not really a question in regard to the language.

    But had you used pow(x, 2), I know some systems where this will probably really optimize down to x*x.
    You have a good point, however, the reverse might not be true. In other words: x*x is unlikely to be deoptimized up to pow(x,2). Perhaps my question would be better put as, "Which is most likely to be faster, and why?"

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But the rule of thumb is: either it's painfully obvious which one is faster even to inexperienced programmers (as in the pow(x, 2) vs x*x case) or even experienced programmers might guess incorrectly. And in any case, for single instructions, it's also nearly irrelevant.

    In the end, whenever there is a question of performance, the first answer is "Run a profiler!". No other answers should be attempted until the profiling data can be analyzed.
    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

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, 03:17 PM
  2. Replies: 5
    Last Post: 12-04-2008, 08: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, 01:48 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM