Your Vector implementation:
Inject operator+() as follows:
Vector Vector::operator+(const Vector &v) { return Vector(x + vx, y + vy, z + vz); }
and add an inline statement to your class definition (this avoids stack hits and popups of return addresses and method arguments for every method call if the compiler finds this useful).
Then add this constructor:
Vector::Vector(const double &x, const double &y, const double &z) : x(x), y(y), z(z) { }
which allows you to create a new vector very efficiently (for example, you would do operator+() in your sentence)!
In the code using your Vector:
You did it:
for(int i = 0; i < N; ++i) { sum = sum + vec[i]; }
Expand this type of loop! Performing only one operation (since it would be optimized for using SSE2 / 3 extensions or something similar) in a very large cycle is very inefficient. You should do something like this:
//Unrolled loop: for(int i = 0; i <= N - 10; i += 10) { sum = sum + vec[i]; + vec[i+1]; + vec[i+2]; + vec[i+3]; + vec[i+4]; + vec[i+5]; + vec[i+6]; + vec[i+7]; + vec[i+8]; + vec[i+9]; } //Doing the "rest": for(int i = (N / 10) * 10; i < N; ++i) { sum = sum + vec[i]; }
(Note that this code has not been verified and may contain a "off-one" -error or so ...)
leemes
source share