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

  1. #31
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    plus if you have come down to the point where you try to optimize such a trivial thing, chances are your doing something horribly wrong somewhere else.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  2. #32
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by CornedBee View Post
    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.
    Sorry, I'm not articulating this very well. Here is some code where += and = will produce the same result in many situations.
    Code:
    			y = x;
    and
    Code:
    			y += m;
    Last edited by thetinman; 06-05-2007 at 08:28 AM. Reason: add the word "is"

  3. #33
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by Shakti View Post
    plus if you have come down to the point where you try to optimize such a trivial thing, chances are your doing something horribly wrong somewhere else.
    The problem is to understand the difference between various numerical methods.

  4. #34
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Sorry, I'm not articulating this very well. Here is some code where += and = will produce the same result in many situations.
    Code:
    			y = x;
    and
    Code:
    			y += m;
    Why do you think that? That would depend entirely on the values of x, y and m. Pick whichever makes sense.

    Suppose you wanted to set y (initalized to 0) equal to x. Which would you do?
    Code:
    y = x;
    Or
    Code:
    y += x;
    I would choose the first, because then both me and others would understand what this code is meant to do.
    I can't remember a single instance where I wondered: hm, should I do assignment or addition? - Eh, I'll pick whichever is faster.
    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).

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sorry tinman, but the point still stands. y = x and y += m do completely different things, so comparing which is faster makes little sense. Use the one that does what you want. If you want to add to y, use +=. If you want to ignore y's current value and assign to it, use =.

    The only question that makes sense is this:
    Code:
    y = 0;
    // x = ?
    y = x;
    versus:
    Code:
    y = 0;
    // x = ?
    y += x;
    Those two code snippets do exactly the same thing (assuming normal addition semantics) only because y is 0 and adding x to 0 is the same as assigning x. However, in that case which version you use would depend on which is clearer given the context, not efficiency concerns.

    Your question leaves out the y = 0, so it makes little sense. For example, if y is 5 and x is 10 you have this:
    Code:
    y = 5;
    x = 10;
    y = x;
    // y is now 10.
    versus:
    Code:
    y = 5;
    x = 10;
    y += x;
    // y is now 15.
    See... it doesn't matter which is faster, because they produce different results.

  6. #36
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by thetinman View Post
    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?
    It's already been mentioned: no.

    But this reminded me of the reverse:
    Code:
    #define ADDRESS 0x400
    
    struct sType1
    {
       unsigned char c;
       unsigned int  i;
    } Object1 = {5,0};
    
    struct sType2
    {
       unsigned char a;
       unsigned char b;
       unsigned char d[50];
       unsigned int  e;
    } Object2;
    
    int foo(void)
    {
       unsigned int addr1, addr2  = ADDRESS + (sizeof(Object2) * (Object1.c - 1));
       addr1  = Object1.c - 1; /* implicit cast of char */
       addr1 *= sizeof(Object2);
       addr1 += ADDRESS;
       return addr1 == addr2;
    }
    Find the [implementation] library function call(s) on the highlighted line.
    Last edited by Dave_Sinkula; 06-05-2007 at 09:46 PM. Reason: After a long struggle to find the right word.
    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. #37
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Sounds like a lea x86 instruction could compute this.
    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

  8. #38
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you are hinting at the multiply, then yes. It was on a platform that either did not have a multiply operation, or had only an 8-bit multiply -- I forget. The multiplication was implemented using a function call.
    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.*

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