与升压::了posix_time :: microsec_clock测量有错误十微秒吗? [英] Measurement with boost::posix_time::microsec_clock has error more than ten microseconds?

查看:309
本文介绍了与升压::了posix_time :: microsec_clock测量有错误十微秒吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

long long unsigned int GetCurrentTimestamp()
{
   LARGE_INTEGER res;
   QueryPerformanceCounter(&res);
   return res.QuadPart;
}


long long unsigned int initalizeFrequency()
{
   LARGE_INTEGER res;
   QueryPerformanceFrequency(&res);
   return res.QuadPart;
}


//start time stamp
boost::posix_time::ptime startTime = boost::posix_time::microsec_clock::local_time();
long long unsigned int start = GetCurrentTimestamp();


// ....
// execution that should be measured
// ....

long long unsigned int end = GetCurrentTimestamp();
boost::posix_time::ptime endTime = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration duration = endTime - startTime;
std::cout << "Duration by Boost posix: " << duration.total_microseconds() <<std::endl;
std::cout << "Processing time is " << ((end - start) * 1000000 / initalizeFrequency()) 
            << " microsec "<< std::endl;

这code的结果是

Duration by Boost posix: 0
Processing time is 24 microsec

为什么有这么大的分歧?加速吸收尽可能多的,因为它应该衡量微秒,但它与微秒错误???

Why there is such a big divergence? Boost sucks as much as it should measure microseconds but it measures microseconds with tenth of microseconds error???

推荐答案

POSIX时间:microsec_clock:

Posix time: microsec_clock:

使用子第二分辨率时钟获得UTC时间。在Unix系统上这是使用gettimeofday的实现。在大多数Win32平台上它使用FTIME实现。 Win32系统往往不能达到通过此API微秒的分辨率。如果更高的分辨率是您的应用程序的关键测试平台上看到所取得的分辨率。

FTIME 根本不提供微秒级分辨率。该参数可以包含单词微秒但实现不提供该范围内的任何精度。它的粒度是在MS制度。

ftime simply does not provide microsecond resolution. The argument may contain the word microsecond but the implementation does not provide any accuracy in that range. It's granularity is in the ms regime.

您会得到一些比你的操作需要更多的时间ZERO不同,说比20ms以上以上。

You'd get something different than ZERO when you operation needs more time, say more than at least 20ms.

编辑:注意:从长远来看,对于Windows的 microsec_clock 实施应的使用的 GetSystemTime preciseAsFileTime功能时可能的(最小REQ。Windows 8桌面,Windows Server 2012中的桌面)来实现微秒级的分辨率。

Note: In the long run the microsec_clock implementation for Windows should use the GetSystemTimePreciseAsFileTime function when possible (min. req. Windows 8 desktop, Windows Server 2012 desktop) to achieve microsecond resolution.

这篇关于与升压::了posix_time :: microsec_clock测量有错误十微秒吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆