QT计算函数运行时间,精确到ms和us

  • Post author:
  • Post category:其他


1、精度为ms级,利用QTime

#include <QTime>

QTime time;

time.start();

youFunction();

qDebug() << time.elapsed()/1000.0 << “s”;

2、精度为us级,利用gettimeofday()

#include <QDebug>

#include <sys/time.h>

struct timeval tpstart,tpend;

float timeUse;

gettimeofday(&tpstart,NULL);

youFunction();

gettimeofday(&tpend,NULL);

timeUse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;

qDebug() << timeUse << “s”;



版权声明:本文为q357010621原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。