<FONT face=Arial>@HiredGoon said:
I'm not arguing that everyone should use += all the time. Premature optimization and all that. It's like putting a biscuit into your mouth in 1 go... sure it saves eating time, but it is messy...
The only time I ever conciously use OP= is in tight loops using overloaded operators in C++ to avoid unneeded copy-constructor calls on big objects (ie a 4x4 matrix). The point is, the original poster pointed out a "WTF" thinking there is no speed gain, while there is a very small one.
This WTF is busted.
</FONT>
<FONT face=Arial>I don't think that's entirely what the original poster was pointing out. There certainly can be a speed gain in the correct use of +=, especially as x gets more complex. But </FONT><FONT face=Arial>the original article says to use the operators "whenever possible." When you start using them repeatedly instead of regular operators, you're now going the wrong direction.</FONT>
<FONT face=Arial>For example:</FONT>
<FONT face="Courier New" size=2>x = a + b + c;</FONT>
<FONT face=Arial> becomes</FONT>
<FONT face="Courier New" size=2>x = a;
x += b;
x += c;</FONT>
<FONT face=Arial>Or the formula most often memorized and then never used again:</FONT>
<FONT size=2><FONT face="Courier New">x = (-b + Math.sqrt(b*b-4*a*c))/(2*a);
</FONT></FONT><FONT size=2> </FONT><FONT face=Arial size=3>becomes, uh...
<FONT face="Courier New" size=2>x = b; x *= b; </FONT></FONT><FONT face="Courier New"><FONT size=3><FONT size=2>z = 4; </FONT></FONT><FONT size=3><FONT size=2>z *=a; z *=c; x -= z; z = Math.sqrt(x);
</FONT></FONT></FONT><FONT face="Courier New"><FONT size=3><FONT size=2>x = -b; x += z; x /= 2; x /= a;
</FONT></FONT></FONT>
<FONT face=Arial size=3>Now, suppose x is a user control... Once the assignment operators are used improperly, speed benefits go out the window. And we've all seen enough literal-minded programmers to know this could conceivably happen. </FONT>
<FONT face=Arial>I think the real problem is the emphasis the article has. If, instead of saying "... as much as you can" and "whenever possible", it just said "consider using for a minor performance increase", nobody would have cared. But the article seems to say that you WILL have faster code if you use 'low-cost' operators. In reality, you might have faster code, but only if it's used correctly, and even then, only by a slight amount.</FONT>