I have a vector class that has addition, multiplication, subtraction, division, etc operators. I'm trying to optimize my program (which does a lot of vector operations) and I'm realizing that about 50% of the time spent is in constructing and destructing vectors. I understand that this is because every time I call a non-compound mathematical operator for a vector, a new vector is constructed. Is there a way to prevent this without using compound operators or expanding vector operations?
If I have:
Vector a = Vector(x, y, z);
Vector b = Vector(a, b, c);
Vector c = a + b;
I can't use += because c is a completely new vector. I know I can speed it up with this:
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
but that doesn't seem as clean as just using an operator.