QT: 一种精确定时器类的实现与使用

news/2024/10/23 9:26:53/

1)类的实现

#ifndef CPRECISETIMER_H
#define CPRECISETIMER_H#include <windows.h>class CPreciseTimer
{
public:CPreciseTimer();bool SupportsHighResCounter();void StartTimer();void StopTimer();__int64 GetTime();private://Auxiliary Functionvoid UpdateElapsed();//Member variablesbool m_bRunning;__int64 m_i64Start;__int64 m_i64Elapsed;//Some auxiliary variables__int64 m_i64Counts;LARGE_INTEGER m_liCount;//Static Variablesstatic bool sm_bInit;static bool sm_bPerformanceCounter;static __int64 sm_i64Freq;
};inline bool CPreciseTimer::SupportsHighResCounter()
{return sm_bPerformanceCounter;
}//Auxiliary Function
inline void CPreciseTimer::UpdateElapsed()
{if(true == sm_bPerformanceCounter){QueryPerformanceCounter(&m_liCount);m_i64Counts = ((__int64)m_liCount.HighPart << 32) + (__int64)m_liCount.LowPart;//Transform in microseconds(m_i64Counts *= 1000000) /= sm_i64Freq;}else//Transform milliseconds to microsecondsm_i64Counts = (__int64)GetTickCount() * 1000;if(m_i64Counts > m_i64Start)m_i64Elapsed = m_i64Counts - m_i64Start;else//Eliminate possible number overflow (0x7fffffffffffffff is the maximal __int64 positive number)m_i64Elapsed = (0x7fffffffffffffff - m_i64Start) + m_i64Counts;
}#endif // CPRECISETIMER_H
#include "cprecisetimer.h"bool CPreciseTimer::sm_bInit = false;
bool CPreciseTimer::sm_bPerformanceCounter;
__int64 CPreciseTimer::sm_i64Freq;//CONSTRUCTOR
CPreciseTimer::CPreciseTimer() : m_i64Start(0), m_i64Elapsed(0), m_bRunning(false)
{//Only if not already initializedif(false == sm_bInit){//Initializing some static variables dependent on the system just onceLARGE_INTEGER liFreq;if(TRUE == QueryPerformanceFrequency(&liFreq)){//Only if the system is supporting High Performancesm_i64Freq = ((__int64)liFreq.HighPart << 32) + (__int64)liFreq.LowPart;sm_bPerformanceCounter = true;}elsesm_bPerformanceCounter = false;sm_bInit = true;}
}void CPreciseTimer::StartTimer()
{if(true == sm_bPerformanceCounter){QueryPerformanceCounter(&m_liCount);m_i64Start = ((__int64)m_liCount.HighPart << 32) + (__int64)m_liCount.LowPart;//Transform in microseconds(m_i64Start *= 1000000) /= sm_i64Freq;}else//Transform milliseconds to microsecondsm_i64Start = (__int64)GetTickCount() * 1000;m_bRunning = true;
}void CPreciseTimer::StopTimer()
{UpdateElapsed();m_bRunning = false;
}__int64 CPreciseTimer::GetTime()
{if(true == m_bRunning)UpdateElapsed();return m_i64Elapsed;
}

2)类的使用

.......

CPreciseTimer preciseTimer;

        // start the timer
        preciseTimer.StartTimer();

//here do something ...

        // Stop the timer
        preciseTimer.StopTimer();

//get the time count

preciseTimer.GetTime();


http://www.ppmy.cn/news/1152793.html

相关文章

软件工程与计算总结(十)软件体系结构设计与构建

目录 ​编辑 一.体系结构设计过程 1.分析关键需求和项目约束 2.选择体系结构风格 3.体系结构逻辑设计 4.体系结构实现 5.完善体系结构设计 6.定义构件接口 二.体系结构原型构建 1.包的创建 2.重要文件的创建 3.定义构件之间的接口 4.关键需求的实现 三.体系结构的…

Java - 发送 HTTP 请求的及其简单的方法模块 - hutool

目录 一、POST 传递简单的字符串内容 .body(params)二、POST 传递 Json 数据&#xff0c;以表单类型传递 .form(params)二、POST 传递 Json 数据&#xff0c;以表单类型传递 .form(params) 和 .body(params) 方法效果等效的思路四、传统接口带 token 验证的代码模板参考链接 一…

为什么手机会莫名多出许多软件?

许多手机用户都曾遭遇过这样的问题&#xff0c;他们在使用手机的过程中&#xff0c;突然发现手机屏幕上出现了一些未知的软件。这些软件并非他们主动下载的&#xff0c;但它们却显现在屏幕上。这些软件从何而来&#xff1f; 其实&#xff0c;这些软件往往是在浏览网页、阅读小…

Chrome 同站策略(SameSite)问题

问题产生 问题复现&#xff1a; A项目页面使用 iframe 引用了B项目 B项目登录页面输入账号密码后点击登录 无法跳转 尝试解决&#xff1a; 在B项目修改了跳转方式 但无论是 this.$router.push 还是 window.herf 都无法实现跳转在iframe中使用 sandbox 沙箱属性 同样无法实现跳…

龙迅LT86102UXE HDMI2.0转二路HDMI2.0/1.4,支持音频剥离,支持4K60HZ

龙迅LT86102UXE 1.描述&#xff1a; 龙迅 LT86102UXE HDMI2.0 分路器具有符合 HDMI2.0/1.4 规范的 1&#xff1a;2 分路器、最大 6Gbps 高速 数据速率、自适应均衡 RX 输入和预加重 TX 输出&#xff08;用于支持长电缆应用&#xff09;、内部 TX 通道交换以 实现灵活的 PCB…

利用人工智能提升企业培训的个性化体验

随着科技的不断进步&#xff0c;人工智能&#xff08;AI&#xff09;正逐渐渗透到各个领域。而在企业培训领域&#xff0c;人工智能也展现出了巨大的潜力。利用人工智能技术的企业培训系统&#xff0c;企业可以为员工提供个性化、高效的培训体验&#xff0c;进一步提升他们的专…

001flutter基础学习

flutter基础学习 参考:https://book.flutterchina.club/chapter1/flutter_intro.html Flutter是谷歌的移动UI框架跨平台: Linux,Android, IOS,Fuchsia原生用户界面:它是原生的,让我们体验更好,性能更好开源免费&#xff1a;完全开源,可以进行商用Flutter与主流框架的对比 Cor…

标定板生成网址,可以直接打印,matlab标定工具箱

Camera Calibration Pattern Generator – calib.io matlab 打开标定的成像 cameraCalibrator 点击完成之后 命令行中输入 cameraParams.IntrinsicMatrix