Speed difference between using int and unsigned int when mixed with doubles

后端 未结 4 2015
鱼传尺愫
鱼传尺愫 2021-01-01 14:22

I have an application where part of the inner loop was basically:

double sum = 0;
for (int i = 0; i != N; ++i, ++data, ++x) sum += *data * x;
4条回答
  •  执念已碎
    2021-01-01 14:29

    I ran this on gcc 4.7.0 on a 64 bit machine running Linux. I replaced the time calls with calls to clock_gettime.

    CPU: Intel X5680 @3.33 GHZ

    GCC flags: -Wall -pedantic -O3 -std=c++11

    Results:

    With int time per operation in ns: 11996, total time sec: 1.57237
    Avg values: 1.06353e+09
    With unsigned int time per operation in ns: 11539, total time sec: 1.5125
    Avg values: 1.06353e+09
    With int time per operation in ns: 11994, total time sec: 1.57217
    Avg values: 1.06353e+09
    

    Evidently on my machine/compiler unsigned is faster.

提交回复
热议问题