方法1:
先包含头文件:
#include<QElapsedTimer>
然后写代码:
QElapsedTimer mstimer;
mstimer.start();
// ****此处添加需要计算运行时间的代码
float time = (double)mstimer.nsecsElapsed()/(double)1000000;
qDebug() << time;
// 最终统计出来是ms
方法2:
利用QTime,其精度为ms级
#include <QDebug>
#include <QTime>
QTime time;
time.start();
// ****此处添加需要计算运行时间的代码
qDebug()<<time.elapsed()/1000.0<<"s";
利用gettimeofday(),其精度为us级
#include <QDebug>
#include <sys/time.h>
struct timeval tpstart,tpend;
float timeuse;
gettimeofday(&tpstart,NULL);
// ****此处添加需要计算运行时间的代码
gettimeofday(&tpend,NULL);
timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;
qDebug()<<timeuse<<"s";
利用clock(),其精度为ms级
#include <QDebug>
#include <sys/time.h>
double time_Start = (double)clock();
// ****此处添加需要计算运行时间的代码
double time_End = (double)clock();
qDebug()<<(time_End - time_Start)/1000.0<<"s";