std::system_clock和之间有什么区别std::steady_clock?(说明不同结果/行为的示例案例会很棒).
如果我的目标是精确测量的功能(如基准)的执行时间,这将是之间是最好的选择std::system_clock,std::steady_clock和std::high_resolution_clock?
我正在使用 Chrono 库进行时间测量。我找到了以下代码并且知道如何使用它。
class Timer {
private:
std::chrono::time_point<std::chrono::high_resolution_clock> pr_StartTime;
std::chrono::time_point<std::chrono::high_resolution_clock> pr_EndTime;
public:
Timer()
{
Start();
}
~Timer()
{
Finish();
}
void Start()
{
pr_StartTime = std::chrono::high_resolution_clock::now();
}
void Finish()
{
pr_EndTime = std::chrono::high_resolution_clock::now();
auto StartTimeMs = std::chrono::time_point_cast<std::chrono::microseconds>(pr_StartTime).time_since_epoch().count();
auto EndTimeMs = std::chrono::time_point_cast<std::chrono::microseconds>(pr_EndTime).time_since_epoch().count();
auto Duration = EndTimeMs - StartTimeMs;
std::cout << "Duration " << Duration << " microseconds." << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么开发人员在转换步骤中使用 time_since_epoch().count() 。为什么我们应该使用 time_since_epoch() 和 count()?