当前位置: 首页 > news >正文

QT中程序执行时间精准计算的三种方法及对比

一.QT程序在提升程序性能的调试中经常要计算一段程序的执行时间,下面介绍两种简单的实现方式,精确度都可以达到ms。

1.方式一

(1)代码:

#include <QDateTime>

qDebug() << "Current_date_and_time  start: " << QDateTime::currentDateTime();  

    for(int f = 0; f< 10000; ++f) {

        std::vector<int> 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::vector<int> 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::vector<int> 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

http://www.lryc.cn/news/279459.html

相关文章:

  • js下载方法分享*
  • C# Stopwatch类_性能_时间计时器
  • 鸿蒙原生应用再添新丁!天眼查 入局鸿蒙
  • HarmonyOS4.0——ArkUI应用说明
  • 基于模块自定义扩展字段的后端逻辑实现(二)
  • 图像中部分RGB矩阵可视化
  • RPA财务机器人在厦门市海沧医院财务管理流程优化汇总的应用
  • 聚焦老年生活与健康,“老有所依·情暖夕阳”元岗街社区微型养老博览会顺利开展
  • 记录汇川:H5U与Factory IO测试12
  • PingCAP 受邀参加 FICC 2023,获 Open100 世纪全球开源贡献奖
  • 10-skywalking告警
  • vue前端开发自学,插槽练习第二次,name属性的使用
  • AI副业拆解:人像卡通化,赋予你的形象全新生命力
  • 宝塔面板安装MySQL8数据库
  • 中科星图——Landsat9_C2_SR大气校正后的地表反射率数据
  • 使用ros_arduino_bridge控制机器人底盘
  • Nacos下载与安装【windows】
  • 【随笔】遗传算法优化的BP神经网络(随笔,不是很详细)
  • Mysql 嵌套子查询
  • Qt QLabel标签控件
  • iOS14 Widget 小组件调研
  • HarmonyOS的应用类型(FA vs Stage)
  • Jeecg创建表单页面步骤
  • leetcode17 电话号码的字母组合
  • 用html和css实现一个加载页面【究极简单】
  • Android-消息机制Handler
  • MySQL夯实之路-事务详解
  • 安泰电子前置微小信号放大器怎么用的
  • 【深度学习每日小知识】Overfitting 过拟合
  • 嵌入式必备的WEB知识