等价的timespec窗户 [英] timespec equivalent for windows

查看:157
本文介绍了等价的timespec窗户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 UNIX 移植我的应用程序窗户和我都碰壁。在我的应用程序需要找到微秒时间(整个应用程序在很大程度上依赖于它,因为它有很高的precision应用程序)。

I am porting my application to windows from unix and 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).

previously我使用的timespec 结构,但窗口不包含这样的事情。命令的GetTickCount 并不足够,因为它以毫秒为单位返回的时间。我也在想 QueryPerformanceFrequency的的。

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

会有人碰巧知道的东西,是等同于的timespec 越好,将来我可能甚至需要纳秒太多,我已经搜查窗户,没有支持。

Would anyone happen to know something that is as identical to timespec as possible, in the future I might even require nanoseconds too, which nothing I have searched in windows supports.

由于任何人的帮助。

推荐答案

见,例如,如何实现长期高使用C ++? C ++定时器功能窗口高分辨率定时提供纳米秒时间。

我已经做了一些测试在Windows XP下Cygwin的:我的机器上,函数gettimeofday()的粒度为约15毫秒(〜六十四分之一秒)。这是相当粗糙。所以,是的粒度:

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:

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

这两个除数1000(POSIX可能有1000000在前)。

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

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

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.

对于Windows的其他possibilites可能是 RDTSC ;看到维基百科的文章。和 HPET ,这不适用于Windows XP中。

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

在Linux中还要注意,时钟()是处理时间,而在Windows上,墙上的时间​​。

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

因此​​,一些code样品,无论是标准的Unix和Windows下运行CYGWIN code,且产生大约50微秒粒度(在我的机)。返回值是在几秒钟内,并给出了距今功能首次被秒数。 (我迟意识到这是一个答案我给过一年前)。

So some sample code, both for standard Unix, and for CYGWIN code running under Windows, which gives a granularity of about 50 microsecs (on my machine). 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).

#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

这篇关于等价的timespec窗户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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