一、精度为us级别
1、方法一
#include <qelapsedtimer.h>
#include <QDebug>
QElapsedTimer mstimer;
mstimer.start()
// 你所要测试的代码块
float time =(double)mstimer.nsecsElapsed()/(double)1000000;
qDebug() <<"time= " <<time<<"ms";// 输出运行时间(ms)
2、gettimeofday()函数
#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()<<"time= "<<timeuse<<"s";
二、、精度为ms级别
1、方法一
#include <QDebug>
#include <QTime>
QTime time;
time.start();
// 你所要测试的代码块
qDebug()<<"time= "<< time.elapsed()/1000.0<<"s";
2、clock()函数
#include <QDebug>
#include <sys/time.h>
double time_Start = (double)clock();
// 你所要测试的代码块
double time_End = (double)clock();
qDebug()<<"time= "<<(time_End - time_Start)/1000.0<<"s";