fmaf
使用该功能而不是使用*
and时,我遇到了巨大的性能下降+
。我在两台 Linux 机器上并使用 g++ 4.4.3 和 g++ 4.6.3
在两台不同的机器上,如果在myOut
不使用fmaf
.
配备 g++ 4.6.3 和 Intel(R) Xeon(R) CPU E5-2650 @ 2.00GHz 的服务器
$ ./a.out fmaf
Time: 1.55008 seconds.
$ ./a.out muladd
Time: 0.403018 seconds.
配备 g++ 4.4.3 和 Intel(R) Xeon(R) CPU X5650 @ 2.67GHz 的服务器
$ ./a.out fmaf
Time: 0.547544 seconds.
$ ./a.out muladd
Time: 0.34955 seconds.
版本不应该fmaf
(除了避免额外的综述然后更精确)更快吗?
#include <stddef.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
int main(int argc, char** argv) {
if (argc != 2) {
std::cout << "missing parameter: 'muladd' or 'fmaf'"
<< std::endl;
exit(-1);
}
struct timeval start,stop,result;
const size_t mySize = 1e6*100;
float* myA = new float[mySize];
float* myB = new float[mySize];
float* myC = new float[mySize];
float* myOut = new float[mySize];
gettimeofday(&start,NULL);
if (!strcmp(argv[1], "muladd")) {
for (size_t i = 0; i < mySize; ++i) {
myOut[i] = myA[i]*myB[i]+myC[i];
}
} else if (!strcmp(argv[1], "fmaf")) {
for (size_t i = 0; i < mySize; ++i) {
myOut[i] = fmaf(myA[i], myB[i], myC[i]);
}
} else {
std::cout << "specify 'muladd' or 'fmaf'" << std::endl;
exit(-1);
}
gettimeofday(&stop,NULL);
timersub(&stop,&start,&result);
std::cout << "Time: " << result.tv_sec + result.tv_usec/1000.0/1000.0
<< " seconds." << std::endl;
delete []myA;
delete []myB;
delete []myC;
delete []myOut;
}