An object memory has a method with the signature
BinaryPattern const& getPattern(unsigned int index) const;
I’m using this in the following for-loop:
for (unsigned int k = 0; k < memory->size(); k++) {
const BinaryPattern s = memory->getPattern(k);
w += s.at(i) * s.at(j);
}
This is very slow. Surprisingly, I found that the following is much faster:
for (unsigned int k = 0; k < memory->size(); k++) {
w += memory->getPattern(k).at(i) * memory->getPattern(k).at(j);
}
“getPattern()” does not do any computations, it pretty much just returns the pattern that is stored in a vector.
Why is it so much slower when I store the reference in a variable? I initially did this to speed things up, as I expected retrieving the reference twice to be slower.
It’s because
makes a copy of the object by calling its copy constructor. Since you don’t want to change it, store a reference instead:
(Since capturing an rvalue with a
constreference extends the rvalue’s lifetime until the reference dies, this even works should the signature ofgetPattern()ever be changed to return a copy, rather than a reference.)In an optimized build (you didn’t compare debug builds, did you?), the compiler might be able to determine that the two calls to the function in
don’t have any side effects and is thus keeping the reference to the object obtained by the first call and optimizing away the second call — arriving at the code I proposed above.
Obviously, the cost of copying a
BinaryPatternobject is not neglectable.