Qt 计算程序运行时间

一、精度为us级别
1、方法一

#include 
#include 

QElapsedTimer mstimer;
mstimer.start()
	
// 你所要测试的代码块
	 
float time =(double)mstimer.nsecsElapsed()/(double)1000000;
qDebug() <<"time= " <

2、gettimeofday()函数

#include 
#include 
 
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= "<

二、、精度为ms级别
1、方法一

#include 
#include 
 
QTime time;
time.start();
 
// 你所要测试的代码块
 
qDebug()<<"time= "<< time.elapsed()/1000.0<<"s";

2、clock()函数

#include 
#include 
 
double time_Start = (double)clock();
 
// 你所要测试的代码块
 
double time_End = (double)clock();
    
qDebug()<<"time= "<<(time_End - time_Start)/1000.0<<"s";

你可能感兴趣的:(qt,开发语言)