Skip to main content
GameDev.net gamedev.net
🔒 Locked

operator+ efficiency?

Started by xropi Oct 25, 2002 at 9:11 AM 5 replies 550+ views
Original Post
xropi
xropi
If I implement basic data structures like n dim vectors I need some operators too. My problem is that if I assign the result to a variable, it needs to be copied then. What can I do to avoid this? I could write the following:
        
inline
void Vec2D_ADD(Vec2D &dest, Vec2D &src1, Vec2D &src2) {
  dest.x = src1.x + src2.x;
  dest.y = src1.y + src2.y;
}
        
which is the best for performance, but is quiet complicated to use. Operators are nice but they are not the best for speed. Any idea on how to write nice and fast code? I've another question: how can I avoid multiple implementation of the same operator+ when I wanna have += operator too? Do I really need to write twice almost the same? [edited by - xropi on October 25, 2002 10:16:44 AM]
I'm interested in full featured 3D system development.
Assassin
Assassin
Use operator += instead of + and = separately.

result = input1 + intput2;
translates to
result = input1;
result += input2;
except the 2nd version doesn''t call the copy constructor unnecessarily
Assassin, aka RedBeard. andyc.org
the_Programmer_Goblin
the_Programmer_Goblin

  
inline Vector2f operator+( const Vector2f &v1 ) const
{
return Vector2f( x + v1.x, y + v1.y );
}


Return-value optimisation will ensure that (in Release builds at the least) there is no copy-constructing unless it cannot be avoided. It can do this because you are "constructing" in the return value of the operator.

I use this code a lot, and have done quite a bit of testing to show that there''s really nothing to worry about if you do it like this. The compiler does a very good job with it.

If you want to know more about it, I''d recommend reading "Efficient C++, performance programming techniques" by Dov Bulka and David Mayhew. There''s also an article on Gamedev that''s reasonably good, covering similar topics, Performance Programming Applied to C++.
xropi
xropi
Thx, it sounds good.

What about my second question?
I'm interested in full featured 3D system development.
davepermen
davepermen
i think boost provides you predefined baseclasses for this, wich have the needed operators defined.. you just have to say something like

struct vec3f : boost::addable { }; and implement, i think, operator += ..

check
www.boost.org

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
the_Programmer_Goblin
the_Programmer_Goblin
quote:
Original post by xropi
What about my second question?



I wouldn''t worry too much about it - it''s an assignment and a return of *this. You could try something like:

*this = *this + otherThing;
return *this;

,but I would definately test the output of that first, because the two *this dereferences could be painful.

I simply do
m_x += otherThing.m_x;
m_y += otherThing.m_y;
return *this;

MagicScript
MagicScript
You can make a templated vector math library that will arrange for very good assembly as output.

Try this

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.