• c++11 std::chrono


    简洁版:项目工程中,可直接调用下列time.h文件

    1. #ifndef TIME_H
    2. #define TIME_H
    3. #include <cstdint>
    4. #include <chrono>
    5. class ElapsedTimer
    6. {
    7. public:
    8. ElapsedTimer(bool do_start = false)
    9. {
    10. if (do_start)
    11. start();
    12. }
    13. void start()
    14. {
    15. m_start_point = std::chrono::high_resolution_clock::now();
    16. }
    17. uint64_t get_elapsed_ms()
    18. {
    19. auto now = std::chrono::high_resolution_clock::now();
    20. return std::chrono::duration_cast<std::chrono::milliseconds>(now-m_start_point).count();
    21. }
    22. uint64_t get_elapsed_us()
    23. {
    24. auto now = std::chrono::high_resolution_clock::now();
    25. return std::chrono::duration_cast<std::chrono::microseconds>(now-m_start_point).count();
    26. }
    27. private:
    28. std::chrono::time_point<std::chrono::high_resolution_clock> m_start_point;
    29. };
    30. #endif

    解析版:

    1. #ifndef TIME_H
    2. #define TIME_H
    3. #include <cstdint>
    4. #include <chrono> //chrono是c++ 11中的时间库,提供计时,时钟等功能
    5. class ElapsedTimer
    6. {
    7. public:
    8. ElapsedTimer(bool do_start = false)
    9. {
    10. if (do_start)
    11. start();
    12. }
    13. void start()
    14. {
    15. m_start_point = std::chrono::high_resolution_clock::now(); //高精度时钟类std::chrono::high_resolution_clock
    16. }
    17. uint64_t get_elapsed_ms()
    18. {
    19. auto now = std::chrono::high_resolution_clock::now(); //要获取某时钟类的当前时刻,调用静态成员函数now()
    20. return std::chrono::duration_cast<std::chrono::milliseconds>(now-m_start_point).count(); //ms;
    21. }
    22. //std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count(); 成员函数count()返回单位时间的数量
    23. uint64_t get_elapsed_us()
    24. {
    25. auto now = std::chrono::high_resolution_clock::now();
    26. return std::chrono::duration_cast<std::chrono::microseconds>(now-m_start_point).count(); //us
    27. }
    28. private:
    29. std::chrono::time_point<std::chrono::high_resolution_clock> m_start_point;
    30. };
    31. #endif

  • 相关阅读:
    辉芒微IO单片机FT60F011A-RB
    CodeTON Round 2补题(A-E)
    生产环境 Nginx后端服务大量TIME-WAIT的解决
    YOLO目标检测——火焰检测数据集+已标注xml和txt格式标签下载分享
    [SpringCloud] Eureka 与 Ribbon 简介
    手把手教你做主成分分析
    有源(AON)和无源(PON)光网络
    HRNet 源码分析
    使用ZIP包安装MySQL及配置教程
    MySQL补充开窗函数
  • 原文地址:https://blog.csdn.net/weixin_42322256/article/details/128012657