Say I have a simple vector class...
Code:
class Vector3{
public:
Vector3();
Vector3(float* data);
Vector3 operator+(const Vector3& vec) const;
Vector3 operator-(const Vector3& vec) const;
float dot(const Vector3& vec) const;
//etc
private:
float m_data[3];
}
In the constructors, the data is simply copied from the input to the private data member. I use this class quite extensively in some code that needs to run fast so performance considerations are important. A common example is something like this...
Code:
float* data;
//initialize and fill data
Vector3 v1(&data[0]), v2(&data[6]), v3(&data[9]);
float result = v1.dot(v2 - v3);
I was wondering if modern compilers would be able to recognize that the operations performed on v1, v2, and v3 are constant and therefore can be performed directly on the variable data without creating new objects. Or, assuming this really is a bottleneck, should I avoid encapsulation here in the name of speed?