Qui*_*ion 6 c c++ windows datetime win32-process
我移植我的应用程序,以windows从unix,我已经碰壁.在我的应用程序中,我需要在几微秒内找到时间(整个应用程序在很大程度上依赖于它,因为它是一个高精度的应用程序).
以前我使用的是timespec结构,但是Windows不包含这样的东西.该命令GetTickCount不够用,因为它以毫秒为单位返回时间.我也在考虑QueryPerformanceFrequency.
有人会碰巧知道尽可能相同的东西timespec吗?
在未来,我甚至可能需要纳秒,我在Windows中搜索的任何内容都不支持.
Jos*_*sey 10
例如,请参阅如何使用C++在Windows上实现长期高分辨率计时?和C++ Timer功能以纳秒为单位提供时间.
我在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))
Run Code Online (Sandbox Code Playgroud)
两个除数都是1000(POSIX可能首先有1000000).
此外,clock_getres(CLOCK_REALTIME,...)返回15毫秒,因此clock_gettime()不太可能有所帮助.并且CLOCK_MONOTONIC和CLOCK_PROCESS_CPUTIME_ID不起作用.
Windows的其他可能性可能是RDTSC ; 请参阅维基百科文章.和HPET,Windows XP不提供.
另请注意,在Linux中,clock()是处理时间,而在Windows中则是挂起时间.
所以一些示例代码,包括标准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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13942 次 |
| 最近记录: |