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.

06-05-2007

thetinman

Quote:

Originally Posted by CornedBee

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;

06-05-2007

thetinman

Quote:

Originally Posted by Shakti

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.

06-05-2007

anon

Quote:

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.

06-05-2007

Daved

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.

Find the [implementation] library function call(s) on the highlighted line.

06-06-2007

CornedBee

Sounds like a lea x86 instruction could compute this.

06-06-2007

Dave_Sinkula

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.