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

  • 相关阅读:
    【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中
    java毕业设计旅游网站设计mybatis+源码+调试部署+系统+数据库+lw
    java程序中如何引入SpringBoot呢?
    【Pandas数据处理100例】(三):DataFrame数据去重,删除重复的行数据
    javascript正则表达式(语法以及正则表达式修饰符)
    两个类文件,实现根据返回的id显示对应的人员信息增强返回
    修改一个MD5的VB源码,使用它支持UTF8编码
    p5.js 3D图形-立方体
    superset连接Apache Spark SQL(hive)过程中的各种报错解决
    dubbo之配置文件
  • 原文地址:https://blog.csdn.net/zkmrobot/article/details/133818783