C++ 性能测试 打印时间类

示例代码

#pragma once
#include 
#include 

class TimeGuard
{
public:
    TimeGuard() 
    {
        d_start_ = std::chrono::high_resolution_clock::now();
    }

    ~TimeGuard()                                                                                                                                                                                             
    {
        stop();
    }

private:
    void stop()
    {
        auto d_end = std::chrono::high_resolution_clock::now();
        auto start = std::chrono::time_point_cast(d_start_)
            .time_since_epoch().count();
        auto end = std::chrono::time_point_cast(d_end)
            .time_since_epoch().count();
        auto duration = end - start;

        double ms = duration * 0.001;
        std::cout << "total:" << ms << "ms\n";
    }

private:
    std::chrono::time_point d_start_;
};

你可能感兴趣的:(C++ 性能测试 打印时间类)