C++ 測試運算快慢

0 測試目標

測試三個計算,是寫在一個等式快,還是分開快。

 

 

1、測試代碼:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <sstream>
// #include <fcntl.h>
using namespace std;

double f(){
    int x=3;
    return 3*100;
}

double g(double y){
    return y*100;
}
double h(double x, double y)
{
    return x*y;
}


int main()
{

    int start = clock();
    double a;
    double b;
    double c;
    for(int i=0;i<1000000000;i++){
            //方案1 
            // c = h(f(),g(f()));
          //方案2 
            a = f();
            b = g(a);
            c = h(a,b);
 

    }

	cout<<" the time cost is"<<(" %.3lf\n", double(clock() - start) / CLOCKS_PER_SEC);

    return 0;



	
}

2 測試結果

 

3、 測試結論 

方案二更快一些

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章