我最近尝试制作一个计算 FLOPS 的简单程序。由于c++
速度足够快,所以我认为值得一试以获得接近的结果。
当我使用 Notepad++ 插件编译它时NppExec
,它工作正常,但我没有构建它。当我在 CodeBlocks 中构建和运行时,它会不断迭代并且不会完成该过程。所以我回到记事本++并再次编译它,然后这次我运行它工作正常,迭代只过了一秒钟。
#include<iostream>
#include<conio.h>
#include<ctime>
#include<iomanip>
using namespace std;
int main(){
float a=1.0,b=2.0,var,j;
double flop;
clock_t start,end;
cout<<"\n Iterating...";
start=clock();
for(j=0;j<999999999;j++){ // Iterates 999999999 times
var=a*b+a/b; // <-- 5 Flops, or am I wrong?
}
end=clock();
cout<<"\n\n Calculating...";
double secs=((float)(end-start))/CLOCKS_PER_SEC;
flop=999999; // Actually is 999999999, but integer overflow in expression
flop=5*(flop*1000+999); // In the brackets I make the value to same as 999999999
// Multiply with 5 and basically get Flops here
flop/=secs; // To get the Flops in second, multiply with time elapsed
string prefix,fstr;
if(flop/1000000000>=1||flop/1000000000<1){
flop/=1000000000;
prefix="GFLOPS";
}
else if(flop/1000000000000>=1){
flop/=1000000000000;
prefix="TFLOPS";
}
cout<<"\n\n\n Floating-points Operations Per Second\n\n > "<<setprecision(3)<<flop<<" "<<prefix;
getch();
return 0;
}
如果您知道如何使结果更精确,请继续,任何答案将不胜感激!