当我编译下面的timer.hpp头文件时,编译器报错:
我猜start和end的变量类型是错误的。正确的类型是什么?我想使用错误:没有找到“operator=”的匹配项(操作数类型为 ‘std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point > >}’和 ‘std::__success_type > >::type {aka std::chrono::duration >}’) end = std::chrono::high_resolution_clock::now() - start;
std::chrono::high_resolution_clock
。#include <chrono>
namespace timer{
static std::chrono::system_clock::time_point start, end;
void initTime(){
start = std::chrono::high_resolution_clock::now();
}
void endTime(){
end = std::chrono::high_resolution_clock::now() - start;
}
}
timer.hpp应该与某个主文件一起使用。
在我想要测量的某个函数之前调用timer::initTime()
,并在函数之后调用timer::endTime()
,就可以得到计时结果(这里省略了持续时间的获取器)。
chrono
的视频教程:https://www.youtube.com/watch?v=P32hvk8b13M 它解决了一些问题。 - Howard Hinnant