I calculate the mean and standard deviation of the elements of a vector. I have two versions of this, and I am completely puzzled why a version using standard algorithms is slower than a version using simple loops.
Both versions use this structure as a return type:
struct MeanAndSigma { double mean; double sigma; };
And the loop version is this:
MeanAndSigma getMeanAndSigma(const DVector& v){ MeanAndSigma ms; ms.mean = 0; for (int i=0;i<v.size();++i){ms.mean += v[i];} ms.mean = ms.mean / v.size(); double sqsum = 0; for (int i=0;i<v.size();++i){sqsum += (v[i]-ms.mean)*(v[i]-ms.mean);} ms.sigma = std::sqrt(sqsum / (v.size()-1)); return ms; }
And the one who has the algorithms:
MeanAndSigma getMeanAndSigma2(const DVector& v){ MeanAndSigma ms; ms.mean = std::accumulate(v.begin(),v.end(),0.0) / v.size(); DVector diff(v.size()); std::transform(v.begin(),v.end(),diff.begin(), std::bind2nd(std::minus<double>(), ms.mean)); double sqsum = std::inner_product(diff.begin(),diff.end(),diff.begin(),0.0); ms.sigma = std::sqrt(sqsum / (v.size()-1)); return ms; }
When I measure the time that they take for 10k calls with a vector with 10k elements, I get ~ 2.0 seconds for the version with loops and ~ 3.2 seconds for one with the algorithms. Why is this?
I already compared processor time and real time, but it seems that both of them work (as expected) on the same processor. Am I stupidly mistaken in using algorithms?
EDIT: I am not saying that the two versions are equivalent. However, I expected the second version to be faster. As stated in the comments and the answer, the second version uses an additional iteration over the elements and an additional DVector (which is btw only a typedef std::vector<double> ). However, I am not familiar with standard algorithms to improve the second version. So now my question is:
How to improve the version with algorithms faster than using simple loops?