为什么std :: accumulation这么慢? [英] Why is std::accumulate so slow?
问题描述
我试图使用简单的for循环,std::accumulate
和手动展开的for循环求和数组元素.如我所料,手动展开循环是最快的循环,但更有趣的是std :: accumulate比简单循环慢得多.
这是我的代码,我使用带有-O3标志的gcc 4.7进行了编译. Visual Studio将需要不同的rdtsc函数实现.
I'm trying to sum array elements using a simple for loop, an std::accumulate
and a manualy unrolled for loop. As I expect, the manually unrolled loop is the fastest one, but more interesting is that std::accumulate is much slower than simple loop.
This is my code, I compiled it with gcc 4.7 with -O3 flag. Visual Studio will need different rdtsc function implementation.
#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
是对整数求和.
因此,您可能要付出转换每个
浮点数加到整数之前.试试:
For starters, your use of std::accumulate
is summing integers.
So you're probably paying the cost of converting each of the
floating point to integer before adding it. Try:
sum = std::accumulate( samples, end, 0.f );
看看这有什么用.
这篇关于为什么std :: accumulation这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!