• 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();

  • 相关阅读:
    ML:基于boston房价数据集利用多种线性回归算法(OLS/PLS/Lasso/Ridge)模型对比实现房价回归预测应用案例实现代码
    SpringBoot整合Spring Security【超详细教程】
    seata 1.5.2 保姆级教程
    【编程题】【Scratch四级】2021.06 从小到大排序
    Java的Atomic原子类
    图像处理Laplacian 算子
    【软考软件评测师】基于规则说明的测试技术下篇
    SSM甜品店系统计算机毕业论文java毕业设计选题源代码
    百度百科怎么修改词条内容?
    合盖计算机后,外接显示器不休眠
  • 原文地址:https://blog.csdn.net/zkmrobot/article/details/133818783