与 Windows 等效的 timespec

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8583308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:42:38  来源:igfitidea点击:

timespec equivalent for windows

c++cwindowsdatetimewin32-process

提问by Quillion

I am porting my application to windowsfrom unixand I have run into a wall. In my application I need to find time in microseconds (the whole application heavily depends on it due to it being a high precision application).

我正在将我的应用程序移植到windowsfromunix并且遇到了障碍。在我的应用程序中,我需要找到以微秒为单位的时间(整个应用程序严重依赖它,因为它是一个高精度应用程序)。

Previously I was using timespecstructure, but windows contains no such thing. The command GetTickCountdoes not suffice because it returns time in milliseconds. I was also thinking of QueryPerformanceFrequency.

以前我使用的是timespec结构,但 windows 不包含这样的东西。该命令GetTickCount还不够,因为它以毫秒为单位返回时间。我也在想QueryPerformanceFrequency

Would anyone happen to know something that is as identical to timespecas possible?

有人会碰巧知道尽可能相同的东西timespec吗?

In the future I might even require nanoseconds too, which nothing I have searched in windows supports.

将来我什至可能也需要纳秒,我在 Windows 中搜索过的任何内容都不支持。

回答by Joseph Quinsey

See, for example, How to realise long-term high-resolution timing on windows using C++?and C++ Timer function to provide time in nano seconds.

例如,请参阅如何使用 C++ 在 windows 上实现长期高分辨率计时?C++ Timer 函数以提供纳秒级的时间

I have done some testing with Cygwin under Windows XP: on my machine, the granularity of gettimeofday() is about 15 msecs (~1/64 secs). Which is quite coarse. And so is the granularity of:

我在 Windows XP 下用 Cygwin 做了一些测试:在我的机器上,gettimeofday() 的粒度大约是 15 毫秒(~1/64 秒)。这是相当粗糙的。粒度也是如此:

* clock_t clock(void) (divisor CLOCKS_PER_SEC)
* clock_t times(struct tms *) (divisor sysconf(_SC_CLK_TCK))

Both divisors are 1000 (POSIX may have 1000000 for first).

两个除数都是 1000(POSIX 首先可能有 1000000)。

Also, clock_getres(CLOCK_REALTIME,...) returns 15 msecs, so clock_gettime() is unlikely to help. And CLOCK_MONOTONIC and CLOCK_PROCESS_CPUTIME_ID don't work.

此外,clock_getres(CLOCK_REALTIME,...) 返回 15 毫秒,因此 clock_gettime() 不太可能有帮助。并且 CLOCK_MONOTONIC 和 CLOCK_PROCESS_CPUTIME_ID 不起作用。

Other possibilites for Windows might be RDTSC; see the Wikipedia article. And HPET, which isn't available with Windows XP.

Windows 的其他可能性可能是RDTSC;参见维基百科文章。和HPET,它不适用于 Windows XP。

Also note in Linux, clock() is the process time, while in Windows it is the wall time.

另请注意,在 Linux 中,clock() 是进程时间,而在 Windows 中,它是挂墙时间。

So some sample code, both for standard Unix, and for CYGWIN code running under Windows, which gives a granularity of about 50 microsecs (on mymachine). The return value is in seconds, and gives the number of seconds elapsed since the function was first called. (I belatedly realized this was in an answer I gave over a year ago).

所以一些示例代码,既适用于标准 Unix,也适用于在 Windows 下运行的 CYGWIN 代码,它提供了大约 50 微秒的粒度(在我的机器上)。返回值以秒为单位,并给出自首次调用函数以来经过的秒数。(我很晚才意识到这是我一年多前给出的答案)。

#ifndef __CYGWIN32__
double RealElapsedTime(void) { // returns 0 seconds first time called
   static struct timeval t0;
   struct timeval tv;
   gettimeofday(&tv, 0);
   if (!t0.tv_sec)
      t0 = tv;
   return tv.tv_sec - t0.tv_sec + (tv.tv_usec - t0.tv_usec) / 1000000.;
}
#else
#include <windows.h>
double RealElapsedTime(void) { // granularity about 50 microsecs on my machine
   static LARGE_INTEGER freq, start;
   LARGE_INTEGER count;
   if (!QueryPerformanceCounter(&count))
      FatalError("QueryPerformanceCounter");
   if (!freq.QuadPart) { // one time initialization
      if (!QueryPerformanceFrequency(&freq))
         FatalError("QueryPerformanceFrequency");
      start = count;
   }
   return (double)(count.QuadPart - start.QuadPart) / freq.QuadPart;
}
#endif

回答by MSalters

Portable between Windows, UNIX, Linux and anything vaguely modern: std::chrono::high_resolution_clock. Resolution may vary, but you can find out at compile time what it is. Nanoseconds is certainly possible on modern hardware.

可在 Windows、UNIX、Linux 和任何模糊现代的东西之间移植:std::chrono::high_resolution_clock. 分辨率可能会有所不同,但您可以在编译时找出它是什么。在现代硬件上,纳秒当然是可能的。

Keep in mind that nanosecond precision really means a sub-meter precision. A nanosecond at lightspeed is only 30 centimeters. Moving your computer from the top of rack to the bottom is literally moving it by several nanoseconds.

请记住,纳秒精度实际上意味着亚米级精度。光速下的纳秒只有 30 厘米。将您的计算机从机架顶部移动到底部实际上就是将其移动几纳秒。