常用的三种获取程序运行时间的方法

  • chrono
    参考教程:http://www.cnblogs.com/jwk000/p/3560086.html
#include     //C++11
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
domeSomething();
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration time_used = chrono::duration_cast::duration>(t2 - t1);
cout<<"solve time cost = "<" secondes."<
  • ctime
#include 
using namespace std;
clock_t start = clock();
// do something...
clock_t end   = clock();
cout << "花费了" << (double)(end - start) / CLOCKS_PER_SEC << "秒" << endl;
//此方法测出来的时间经常不准,不建议使用
  • gettimeofday
#include 
using namespace std;
timeval start, end;
gettimeofday(&start, NULL);
// do somethind
gettimeofday(&end, NULL);
cout << "花费了" << end.tv_sec - start.tv_sec << " s." << endl;
cout << "花费了" << end.tv_usec - start.tv_usec << " us." << endl;

你可能感兴趣的:(C++)