High_resolution_clock的时间测量没有按预期工作
Time measurements with High_resolution_clock not working as intended
我希望能够使用我的Clock类测量经过的时间(对于帧时间)。(问题描述如下代码)
Clock.h
typedef std::chrono::high_resolution_clock::time_point timePt;
class Clock
{
timePt currentTime;
timePt lastTime;
public:
Clock();
void update();
uint64_t deltaTime();
};
Clock.cpp
#include "Clock.h"
using namespace std::chrono;
Clock::Clock()
{
currentTime = high_resolution_clock::now();
lastTime = currentTime;
}
void Clock::update()
{
lastTime = currentTime;
currentTime = high_resolution_clock::now();
}
uint64_t Clock::deltaTime()
{
microseconds delta = duration_cast<microseconds>(currentTime - lastTime);
return delta.count();
}
当我尝试使用时钟像这样
Clock clock;
while(1) {
clock.update();
uint64_t dt = clock.deltaTime();
for (int i=0; i < 10000; i++)
{
//do something to waste time between updates
int k = i*dt;
}
cout << dt << endl; //time elapsed since last update in microseconds
}
对我来说,它打印了大约30次"0",直到最后打印出一个总是非常接近"15625"微秒(15.625毫秒)的数字。
我的问题是,为什么没有介于两者之间?我想知道我的实现是错误的还是high_resolution_clock上的精度表现得很奇怪。什么好主意吗?
编辑:我在windows 8计算机上使用Codeblocks与mingw32编译器。
EDIT2:我尝试运行以下代码,应该显示high_resolution_clock精度:
template <class Clock>
void display_precision()
{
typedef std::chrono::duration<double, std::nano> NS;
NS ns = typename Clock::duration(1);
std::cout << ns.count() << " nsn";
}
int main()
{
display_precision<std::chrono::high_resolution_clock>();
}
对我来说,它打印:"1000 ns"。我猜high_resolution_clock的精度是1微秒?但在我的测试中,它的精度似乎只有16毫秒。
您使用的是什么系统?(我猜是Windows吧?Visual Studio已知有这个问题,现在在VS 2015中修复了,见bug报告)。在一些系统上,high_resolution_clock
被定义为system_clock
的别名,它的分辨率很低,比如你看到的16毫秒。请看下面这个问题。
我在Windows 10上使用msys2也有同样的问题:对于我测试的大多数子函数,返回的delta是0,突然返回15xxx或24xxx微秒。我认为我的代码有问题,因为所有的教程都没有提到任何问题。对于time.h中的difftime(finish, start)
也是如此,它通常返回0。
我终于把我所有的high_resolution clock
换成了steady_clock
,我可以找到合适的时间:
auto t_start = std::chrono::steady_clock::now();
_cvTracker->track(image); // my function to test
std::cout << "Time taken = " << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock ::now() - t_start).count() << " microseconds" << std::endl;
// returns the proper value (or at least a plausible value)
而这返回的大多是0:
auto t_start = std::chrono::high_resolution_clock::now();
_cvTracker->track(image); // my function to test
std::cout << "Time taken = " << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t_start).count() << " microseconds" << std::endl;
// returns 0 most of the time
difftime
似乎也不起作用:
time_t start, finish;
time(&start);
_cvTracker->track(image);
time(&finish);
std::cout << "Time taken= " << difftime(finish, start) << std::endl;
// returns 0 most of the time
- QSqlquery prepare()和bindvalue()不工作
- 在C++中测量postscript字体宽度
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- 模式匹配函数的时间测量无法正常工作
- High_resolution_clock的时间测量没有按预期工作