【发布时间】:2015-03-14 00:40:28
【问题描述】:
我编写这段代码是为了测试特征加法与普通旧标量加法的性能。
int x, y;
cin >> x; cin >> y;
typedef int theType;
Array<theType, 8, 1> theArray; theArray << 0,0,0,0,0,0,0,0;
StopWatch sw;
sw.Start();
for(int k = 0; k < y*1000000; k++){
theArray << 0,0,0,0,0,0,0,0;
for (int i = 0; i < 10 *x; i++){
theArray += 2;
}
}
sw.Stop();
cout << theArray << " : " << sw.MilliSeconds() << endl;
theType f = 0;
sw.Start();
for(int k = 0; k < y*1000000; k++){
f = f-1;
for (int i = 0; i < 80 * x; i++){
f += 2;
}
}
sw.Stop();
cout << f << " : " << sw.MilliSeconds();
我正在使用 g++ -O2 运行该代码。我用命令行设置 x 和 y 并将它们用作 for 循环的上限,因此编译器不会优化 for 循环。特征测试生成一个包含 8 个值的数组,并明智地添加一个常数分量。标量测试只是增加一个标量值,但它的完成量是特征测试的 8 倍。
结果(使用 x = 1, y=1):
使用 int 作为类型:52 ms Eigen vs. 1ms scalar
使用 short 作为类型:54 ms Eigen vs. 1ms scalar
为什么 Eigen 变慢了?由于在 eigen 中使用了 SIMD,我预计它会更快一些。 eigen 真的这么慢,还是我做错了什么?
【问题讨论】: