Quote:Original post by johnstanpThis is about what I would expect. With the two-statement version, there is an explicit copy from v2 to v1, but with the single-statement version, when RVO is applied, there is no explicit copy just direct assigning of the result.Quote:I'm hesitant to state for certain one way or another about this, since I don't know exactly what your compiler does [of course] or how well its various features are implemented, or even what compiler it is, but any compiler with a reasonable implementation of return value optimization will easily handle the above example.
In fact with my compiler( GNU G++ ), the expression v1 = v2 + v3 is computed faster than v1 = v2; v1 += v3. It is computed 1.5 times faster.
I am quite surprised by the difference of speed, but pleased.
What is certain though is that v1 += v2; would have to be no slower than v1 = v1 + v2; That's where it is definitely better to use +=
But if you put a line like v1 = v0; before it, then you've just lots any advantage in using +=.
So, the good news is that the simplest option turns out to be the fastest.