电脑做网站空间,交换链接适用于哪些网站,哪些网站做的美剧,哪些网站可以免费做h5一.QT程序在提升程序性能的调试中经常要计算一段程序的执行时间#xff0c;下面介绍两种简单的实现方式#xff0c;精确度都可以达到ms。
1.方式一
#xff08;1#xff09;代码#xff1a;
#include QDateTime
qDebug() Current_date_and_tim…一.QT程序在提升程序性能的调试中经常要计算一段程序的执行时间下面介绍两种简单的实现方式精确度都可以达到ms。
1.方式一
1代码
#include QDateTime
qDebug() Current_date_and_time start: QDateTime::currentDateTime(); for(int f 0; f 10000; f) { std::vectorint vec(10000); for (int i 0; i 10000; i) { vec[i] i; } }
qDebug() Current_date_and_time end: QDateTime::currentDateTime();
2测试结果
Current_date_and_time start: QDateTime(2024-01-13 10:10:33.084 中国标准时间 Qt::LocalTime)
Current_date_and_time end: QDateTime(2024-01-13 10:10:33.440 中国标准时间 Qt::LocalTime)
手动计算差值为356ms
2.方式二
1代码 qint64 time1 QDateTime::currentDateTime().toMSecsSinceEpoch(); for(int f 0; f 10000; f) { std::vectorint vec(10000); for (int i 0; i 10000; i) { vec[i] i; } } qint64 time2 QDateTime::currentDateTime().toMSecsSinceEpoch(); qDebug() Time difference in milliseconds: time2 - time1;
2测试结果
Time difference in milliseconds: 344
3.方式三
1代码
#include QElapsedTimer
QElapsedTimer timer;
int timervalue timer.elapsed();
qDebug() Current_date_and_time start: timer.elapsed();
for(int f 0; f 10000; f) { std::vectorint vec(10000); for (int i 0; i 10000; i) { vec[i] i; }
}
qDebug() Current_date_and_time end: timer.elapsed();
qDebug() time total: timer.elapsed() - timervalue ;
2测试结果
Current_date_and_time start: 59181610
Current_date_and_time end: 59181962
time total: 352
二.总结
可以看到上述三种方法同一段程序得到的时间并不完全一致经过多次测试验证每种方法
时间都是变化的单位ms 次数 方式一 方式二 方式三 1 356 344 352 2 348 346 348 3 351 345 352 4 344 342 347