• C++消息总线Mozart:timer类实现


    Timer类功能是计算一个函数的执行耗时,Timer的完整代码路径如下:
    https://github.com/libmozart/foundation/blob/master/mozart%2B%2B/mpp_foundation/timer.hpp

    Timer的耗时测试举例如下:

    int test_epoch = 10000000;
    
    std::cout << "[Small Data] std::any copying: " << mpp::timer::measure([]() {
        std::any a(10);
        for (int i = 0; i < test_epoch; ++i)
            std::any b(a);
    }) << std::endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    timer的接口包括:reset,time,delay,measure等四个接口,而且timer的所有接口均是静态接口。

    reset接口

    reset接口功能是重置基准时间。函数定义如下:

    // 基准时间成员变量
    static std::chrono::time_point<std::chrono::high_resolution_clock> m_timer;
    
    static void reset() 
    {
        // 重置基准时间
        m_timer = std::chrono::high_resolution_clock::now();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    time接口

    获取当前时间相对与基准时间的偏移值。其定义如下:

    static size_t time(time_unit unit = time_unit::milliseconds)
    {
        switch (unit) 
        {
            case time_unit::nanoseconds:
                return std::chrono::duration_cast<std::chrono::nanoseconds>(
                        std::chrono::high_resolution_clock::now() - m_timer)
                        .count();
            case time_unit::microseconds:
                return std::chrono::duration_cast<std::chrono::microseconds>(
                        std::chrono::high_resolution_clock::now() - m_timer)
                        .count();
            case time_unit::milliseconds:
                return std::chrono::duration_cast<std::chrono::milliseconds>(
                        std::chrono::high_resolution_clock::now() - m_timer)
                        .count();
            case time_unit::seconds:
                return std::chrono::duration_cast<std::chrono::seconds>(
                        std::chrono::high_resolution_clock::now() - m_timer)
                        .count();
            case time_unit::minutes:
                return std::chrono::duration_cast<std::chrono::minutes>(
                        std::chrono::high_resolution_clock::now() - m_timer)
                        .count();
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    measure接口

    函数耗时测量接口,const function &func 为待测试的函数对象,time_unit unit = time_unit::milliseconds 耗时时间单位。measure支持5中测量单位,他们分别为纳秒,微秒,毫秒,秒和分。其中时间单位定义如下:

    enum class time_unit 
    {
        nanoseconds,  // 纳秒
        microseconds, // 微秒
        milliseconds, // 毫秒
        seconds,      // 秒
        minutes       // 分
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    measure 接口定义如下:

    static size_t measure(const function<void()> &func, time_unit unit = time_unit::milliseconds) 
    {
        size_t begin(0), end(0);
        begin = time(unit);  // 相对于基准时间的起始偏移
        func();
        end = time(unit);    // 相对于基准时间的终止偏移
        return end - begin;  // 终止偏移 - 起始偏移 = 函数耗时
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    delay接口

    延时指定时间间隔,入参包括延时时间(size_t time)和延时时间单位(time_unit unit = time_unit::milliseconds默认毫秒),其定义如下:

    static void delay(size_t time, time_unit unit = time_unit::milliseconds) 
    {
        switch (unit) 
        {
            case time_unit::nanoseconds:
                std::this_thread::sleep_for(std::chrono::nanoseconds(time));
                break;
            case time_unit::microseconds:
                std::this_thread::sleep_for(std::chrono::microseconds(time));
                break;
            case time_unit::milliseconds:
                std::this_thread::sleep_for(std::chrono::milliseconds(time));
                break;
            case time_unit::seconds:
                std::this_thread::sleep_for(std::chrono::seconds(time));
                break;
            case time_unit::minutes:
                std::this_thread::sleep_for(std::chrono::minutes(time));
                break;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    总结

    timer是Mozart的应用接口,作用是测试相关函数的执行耗时。一般我们只需要关注timer::measure接口即可。

  • 相关阅读:
    OOP设计原则详解
    单例模式、工厂模式 c++关键字 static
    Python之PyMySQL操作详解
    深度学习修炼(二)全连接神经网络 | Softmax,交叉熵损失函数 优化AdaGrad,RMSProp等 对抗过拟合 全攻略
    牛客Leetcode高频题解(下)
    VF01销售开票发票金额控制增强
    CI/CD持续集成/持续部署
    ABAP 选择屏幕多页签
    【WSN定位】基于chan算法、fang算法、taylor算法实现目标定位附Matlab代码
    离散卡尔曼滤波器算法详解及重要参数(Q、R、P)的讨论
  • 原文地址:https://blog.csdn.net/liuguang841118/article/details/132701051