前一段時間我聽說有些編譯器使用SSE2擴展來實現x86_64架構的浮點運算,所以我使用這個簡單的代碼來確定它們之間的性能差異。x86和x86_64中的float和double之間的性能差異
我通過BIOS禁用了Intel SpeedStep技術,系統負載大致相當於我的測試。我在OpenSuSE 64位上使用GCC 4.8。
我正在寫一個程序有很多FPU操作,我想知道這個測試是否有效?
並瞭解每個架構下關於float
和double
之間性能差異的任何信息。
代碼:
#include <iostream>
#include <sys/time.h>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
timeval t1, t2;
double elapsedTime;
double TotalTime = 0;
for(int j=0 ; j < 100 ; j++)
{
// start timer
gettimeofday(&t1, NULL);
vector<float> RealVec;
float temp;
for (int i = 0; i < 1000000; i++)
{
temp = static_cast <float> (rand())/(static_cast <float> (RAND_MAX));
RealVec.push_back(temp);
}
for (int i = 0; i < 1000000; i++)
{
RealVec[i] = (RealVec[i]*2-435.345345)/15.75;
}
// stop timer
gettimeofday(&t2, NULL);
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec)/1000.0; // us to ms
TotalTime = TotalTime + elapsedTime;
}
cout << TotalTime/100 << " ms.\n";
return 0;
}
和結果:
32位雙
157.781毫秒。 151.994 ms。 152.244 ms。
32 Bit Float
149.896 ms。 148.489 ms。 161.086 ms。
64位雙倍
110.125毫秒。 111.612 ms。 113.818毫秒。
64位浮點數
110.393 ms。 106.778 ms。 107.833 ms。
LOL @「flouting point orations」.... – twalberg 2015-02-05 18:32:56