C ++在Windows中高precision时间测量 [英] C++ high precision time measurement in Windows

查看:173
本文介绍了C ++在Windows中高precision时间测量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的测量特定的时间点下跌到纳秒在Windows中使用C ++。这可能吗?如果不是,是有可能得到至少在微秒的具体时间?任何图书馆应该做的,除非我想这可能与托管code。
谢谢

I'm interested in measuring a specific point in time down to the nanosecond using C++ in Windows. Is this possible? If it isn't, is it possible to get the specific time in microseconds at least?. Any library should do, unless I suppose it's possible with managed code. thanks

推荐答案

如果你有一个多核计算机 QueryPerformanceCounter的上运行的线程应用程序可以(而且会)返回不同的值这取决于核心code正在执行上。请参见的MSDN文章。 ( RDTSC 有同样的问题)

If you have a threaded application running on a multicore computer QueryPerformanceCounter can (and will) return different values depending on which core the code is executing on. See this MSDN article. (rdtsc has the same problem)

这不仅是一个理论问题;我们遇到了它与我们的应用,不得不得出结论,唯一可靠的时间源 timeGetTime 其中只有毫秒precision(幸好是在我们的情况足够了)。我们也尝试行了吧线程亲和力为我们的线程,以保证每个线程始终得到了来自 QueryPerformanceCounter的一致的值,这工作,但它绝对打死在应用程序的性能。

This is not just a theoretical problem; we ran into it with our application and had to conclude that the only reliable time source is timeGetTime which only has ms precision (which fortunately was sufficient in our case). We also tried fixating the thread affinity for our threads to guarantee that each thread always got a consistent value from QueryPerformanceCounter, this worked but it absolutely killed the performance in the application.

要总结的东西了没有一个的可靠的窗户上的计时器,可以用来时间的事情与微秒precision(至少不是一个多核计算机上运行时)。

To sum things up there isn't a reliable timer on windows that can be used to time thing with micro second precision (at least not when running on a multicore computer).

这篇关于C ++在Windows中高precision时间测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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