这个问题已经在这里有了答案: > Why is accumulate faster than a simple for cycle? 3个
我正在尝试使用简单的for循环,std :: accumulate和手动展开的for循环求和数组元素.如我所料,手动展开循环是最快的循环,但更有趣的是std :: accumulate比简单循环慢得多.
这是我的代码,我使用带有-O3标志的gcc 4.7进行了编译. Visual Studio将需要不同的rdtsc函数实现.
#include <iostream>
#include <algorithm>
#include <numeric>
#include <stdint.h>
using namespace std;
__inline__ uint64_t rdtsc() {
uint64_t a, d;
__asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
return (d<<32) | a;
}
class mytimer
{
public:
mytimer() { _start_time = rdtsc(); }
void restart() { _start_time = rdtsc(); }
uint64_t elapsed() const
{ return rdtsc() - _start_time; }
private:
uint64_t _start_time;
}; // timer
int main()
{
const int num_samples = 1000;
float* samples = new float[num_samples];
mytimer timer;
for (int i = 0; i < num_samples; i++) {
samples[i] = 1.f;
}
double result = timer.elapsed();
std::cout << "rewrite of " << (num_samples*sizeof(float)/(1024*1024)) << " Mb takes " << result << std::endl;
timer.restart();
float sum = 0;
for (int i = 0; i < num_samples; i++) {
sum += samples[i];
}
result = timer.elapsed();
std::cout << "naive:\t\t" << result << ", sum = " << sum << std::endl;
timer.restart();
float* end = samples + num_samples;
sum = 0;
for(float* i = samples; i < end; i++) {
sum += *i;
}
result = timer.elapsed();
std::cout << "pointers:\t\t" << result << ", sum = " << sum << std::endl;
timer.restart();
sum = 0;
sum = std::accumulate(samples, end, 0);
result = timer.elapsed();
std::cout << "algorithm:\t" << result << ", sum = " << sum << std::endl;
// With ILP
timer.restart();
float sum0 = 0, sum1 = 0;
sum = 0;
for (int i = 0; i < num_samples; i+=2) {
sum0 += samples[i];
sum1 += samples[i+1];
}
sum = sum0 + sum1;
result = timer.elapsed();
std::cout << "ILP:\t\t" << result << ", sum = " << sum << std::endl;
}
解决方法:
对于初学者,您对std :: accumulate的使用是对整数求和.
因此,您可能要付出转换每个
浮点数添加到整数之前.尝试:
sum = std::accumulate( samples, end, 0.f );
看看那没什么不同.