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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kunyXu/article/details/78864473
#include <chrono>    //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<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
cout<<"solve time cost = "<<time_used.count()<<" secondes."<<endl;
  • ctime
#include <ctime>
using namespace std;
clock_t start = clock();
// do something...
clock_t end   = clock();
cout << "花费了" << (double)(end - start) / CLOCKS_PER_SEC << "秒" << endl;
//此方法测出来的时间经常不准,不建议使用
  • gettimeofday
#include <time.h>
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;

猜你喜欢

转载自blog.csdn.net/kunyXu/article/details/78864473
今日推荐