示例代碼
#pragma once
#include <chrono>
#include <iostream>
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<std::chrono::microseconds>(d_start_)
.time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(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<std::chrono::high_resolution_clock> d_start_;
};