0

I wrote a class like this:

class vector3D{
    public:
    double x,y,z;
}

and i overload + operator:

class vector3D{
    public:
    double x,y,z;
    vector3D operator+(const vector3D &lhs)
    {
        vector3D temp(x+lhs.x,y+lhs.y,z+lhs.z);
        return temp;
    }
}

but using C=A+B is slower than

C.x = A.x + B.x;
C.y = A.y + B.y;
C.z = A.z + B.z;

i think it is because defining a vector3D instance in + overloading function. is there anyway to avoid this?

(FYI: i build with this flags: -Wall -Wextra -Wunreachable-code -m32 -DNDEBUG -O2 -D_LARGEFILE64_SOURCE -D_REETRANT -D_THREAD_SAFE)

EDIT: this is how i test speed of two approach(in main() ):

//test bench
vector3D A(0.0,0.0,0.0), B(1e-9,1e-9,1e-9);
clock_t c = clock();
//case A
for(long int i=0;i<1000000000;i++)
{
    A = A + B;
}
cout<<"case A took: "<<1.0*(clock()-c)/CLOCKS_PER_SEC<<A<<endl;
c = clock();
//case B
for(long int i=0;i<1000000000;i++)
{
    A._x = A._x+B._x;
    A._y = A._x+B._y;
    A._z = A._x+B._z;
}
cout<<"case B took: "<<1.0*(clock()-c)/CLOCKS_PER_SEC<<A<<endl;

and the result is:

case A took: 5.539[1, 1, 1]
case B took: 1.531[2, 2, 2]
samad montazeri
  • 1,203
  • 16
  • 28
  • I'm not sure what you mean. Your `operator+` function is slower than those three lines that just add the components together, yes, but that function is doing a different thing from just those addition statements. – Sam Estep Jul 04 '15 at 17:31
  • But i only want addition functionality! and i don't think repeating that three line over and over in my code would be wise... there must be a better way. – samad montazeri Jul 04 '15 at 17:39
  • In your edited question, you're assigning to the member variables in `C`; what is `C`? Where do you declare and initialize it? – Sam Estep Jul 04 '15 at 17:43
  • :) you are right. but what i meant was C=A+B; , .... i edited my question. – samad montazeri Jul 04 '15 at 17:45
  • You may be interested in [this post](http://stackoverflow.com/questions/17473753/c11-return-value-optimization-or-move) on return value optimization. – forkrul Jul 04 '15 at 17:47

2 Answers2

1

Since you're creating an additional object, it will carry some overhead. This is inherent.

However, looking at the "payload lines" you want, they are very similar to what you'd have in the body of operator+= adding some other:

vector3D &operator+=(const vector3D &other) // Note - no objects created.
{
    x += other.x;
    y += other.y;
    z += other.z;

    return *this; // Note - return by reference.
}

Of course, operator+= modifies its left operand (which is exactly why you have operator+ with its object-creation overhead).

In general, you should prefer operator+= for heavy objects where it is applicable (see also this question).

Community
  • 1
  • 1
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
0

You may actually get a performance boost by collapsing this to one line and taking advantage of return value optimization:

vector3D operator+(const vector3D &lhs)
    {
        return vector3D(x+lhs.x,y+lhs.y,z+lhs.z);
    }
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186