int a, b, c;
b += c;
a = b;
int a, b, c;
a = b + c;
int a, b, c;
b += c;
a = b;
int a, b, c;
a = b + c;
Quote:
Original post by Roboguy
Those two bits of code don't do the same thing. But, anyway, as SiCrane said, it doesn't matter.
Quote:
Original post by DarkZoulz Quote:
Original post by Roboguy
Those two bits of code don't do the same thing. But, anyway, as SiCrane said, it doesn't matter.
They don't? But the result is the same? First one: adds 'c' to 'b' and then sets 'a' to 'b'. The second: sets 'a' to 'b + c'. Maybe the += operator is slow? (or slower)
I know this tiny optimization if most likely insignificant, but would be fun to know.
T operator+(const T & a, const T & b) { T c(a); return c += b;}T operator+(const T & a, const T & b) { T c(a); c += b; return c;}
Quote:What code did you use to profile? Was it release mode? How are you making sure the variables aren't optimised away? They should have pretty much identical performance with a release build with full optimisation.
Original post by DarkZoulz
How about this:
*** Source Snippet Removed ***
Why is alt 1 faster than 2? I've tried it. Testing each alternative in a for-loop of 10,000,000 cycles. I used GetTickCount() for the timing.
Alt 1: ~460 ms
Alt 2: ~840 ms
Quote:
Original post by Sharlin
Hmm... AFAIK the suggested way of writing operator+ (and others) is as below, and the reason is exactly that in this case, the compiler is allowed to do NRVO and create the return value in place, thus eliminating a copy. Of course, DirectX might not follow this idiom, or optimizations might not have been enabled...
D3DXINLINE D3DXVECTOR3D3DXVECTOR3::operator + ( CONST D3DXVECTOR3& v ) const{ return D3DXVECTOR3(x + v.x, y + v.y, z + v.z);}D3DXINLINE D3DXVECTOR3&D3DXVECTOR3::operator += ( CONST D3DXVECTOR3& v ){ x += v.x; y += v.y; z += v.z; return *this;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more