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


    1)类的实现

    1. #ifndef CPRECISETIMER_H
    2. #define CPRECISETIMER_H
    3. #include <windows.h>
    4. class CPreciseTimer
    5. {
    6. public:
    7. CPreciseTimer();
    8. bool SupportsHighResCounter();
    9. void StartTimer();
    10. void StopTimer();
    11. __int64 GetTime();
    12. private:
    13. //Auxiliary Function
    14. void UpdateElapsed();
    15. //Member variables
    16. bool m_bRunning;
    17. __int64 m_i64Start;
    18. __int64 m_i64Elapsed;
    19. //Some auxiliary variables
    20. __int64 m_i64Counts;
    21. LARGE_INTEGER m_liCount;
    22. //Static Variables
    23. static bool sm_bInit;
    24. static bool sm_bPerformanceCounter;
    25. static __int64 sm_i64Freq;
    26. };
    27. inline bool CPreciseTimer::SupportsHighResCounter()
    28. {
    29. return sm_bPerformanceCounter;
    30. }
    31. //Auxiliary Function
    32. inline void CPreciseTimer::UpdateElapsed()
    33. {
    34. if(true == sm_bPerformanceCounter)
    35. {
    36. QueryPerformanceCounter(&m_liCount);
    37. m_i64Counts = ((__int64)m_liCount.HighPart << 32) + (__int64)m_liCount.LowPart;
    38. //Transform in microseconds
    39. (m_i64Counts *= 1000000) /= sm_i64Freq;
    40. }
    41. else
    42. //Transform milliseconds to microseconds
    43. m_i64Counts = (__int64)GetTickCount() * 1000;
    44. if(m_i64Counts > m_i64Start)
    45. m_i64Elapsed = m_i64Counts - m_i64Start;
    46. else
    47. //Eliminate possible number overflow (0x7fffffffffffffff is the maximal __int64 positive number)
    48. m_i64Elapsed = (0x7fffffffffffffff - m_i64Start) + m_i64Counts;
    49. }
    50. #endif // CPRECISETIMER_H
    1. #include "cprecisetimer.h"
    2. bool CPreciseTimer::sm_bInit = false;
    3. bool CPreciseTimer::sm_bPerformanceCounter;
    4. __int64 CPreciseTimer::sm_i64Freq;
    5. //CONSTRUCTOR
    6. CPreciseTimer::CPreciseTimer() : m_i64Start(0), m_i64Elapsed(0), m_bRunning(false)
    7. {
    8. //Only if not already initialized
    9. if(false == sm_bInit)
    10. {
    11. //Initializing some static variables dependent on the system just once
    12. LARGE_INTEGER liFreq;
    13. if(TRUE == QueryPerformanceFrequency(&liFreq))
    14. {
    15. //Only if the system is supporting High Performance
    16. sm_i64Freq = ((__int64)liFreq.HighPart << 32) + (__int64)liFreq.LowPart;
    17. sm_bPerformanceCounter = true;
    18. }
    19. else
    20. sm_bPerformanceCounter = false;
    21. sm_bInit = true;
    22. }
    23. }
    24. void CPreciseTimer::StartTimer()
    25. {
    26. if(true == sm_bPerformanceCounter)
    27. {
    28. QueryPerformanceCounter(&m_liCount);
    29. m_i64Start = ((__int64)m_liCount.HighPart << 32) + (__int64)m_liCount.LowPart;
    30. //Transform in microseconds
    31. (m_i64Start *= 1000000) /= sm_i64Freq;
    32. }
    33. else
    34. //Transform milliseconds to microseconds
    35. m_i64Start = (__int64)GetTickCount() * 1000;
    36. m_bRunning = true;
    37. }
    38. void CPreciseTimer::StopTimer()
    39. {
    40. UpdateElapsed();
    41. m_bRunning = false;
    42. }
    43. __int64 CPreciseTimer::GetTime()
    44. {
    45. if(true == m_bRunning)
    46. UpdateElapsed();
    47. return m_i64Elapsed;
    48. }

    2)类的使用

    .......

    CPreciseTimer preciseTimer;

            // start the timer
            preciseTimer.StartTimer();

    //here do something ...

            // Stop the timer
            preciseTimer.StopTimer();

    //get the time count

    preciseTimer.GetTime();

  • 相关阅读:
    大数据架构:字节跳动开源Cloud Shuffle Service简介
    [附源码]JAVA毕业设计机房预约系统(系统+LW)
    Maven依赖管理项目构建工具(保姆级教学---下篇)
    【2023-10-12】如何保证代码质量
    fatal error C1083: 无法打开包括文件: “stdafx.h”: No such file or directory
    Jenkins--部署--01--打包Maven项目为Docker镜像并运行
    Python中图像相似性度量方法汇总
    谷粒商城 高级篇 (十三) --------- 异步&线程池
    mysql限制用户登录失败次数,限制时间
    CrossOver22试用期到了如何免费使用?
  • 原文地址:https://blog.csdn.net/zkmrobot/article/details/133818783