我应该用什么来取代Windows上的gettimeofday()?

我正在编写一个可移植的Socket类,它支持发送和接收超时…为了实现这些超时,我使用了select() ….但是,我有时需要知道在select()这当然在Linux上,我会通过调用gettimeofday()之前和之后调用select()然后使用timersub()来计算delta …实现…

鉴于Windows上的select()接受struct timeval超时,我应该使用什么方法来取代Windows上的gettimeofday()?

Solutions Collecting From Web of "我应该用什么来取代Windows上的gettimeofday()?"

我最终找到了这个页面:在窗口gettimeofday()[编辑:链接被删除,因为它现在指向一个广告网站]。 在Windows上有gettimeofday()方便,实用的实现。 它使用GetSystemTimeAsFileTime()方法获得精确的时钟。

更新 :这是一个活跃的链接[编辑:链接被删除,因为它现在指向一个广告网站],指向OP所指的实施。 还要注意,链接的实现中存在拼写错误:

 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 // WRONG #else #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL // WRONG #endif 

显示的值在最后缺少一个额外的0 (他们假定为微秒,而不是100纳秒的间隔)。 这个错字是通过Google代码项目页面上的这个评论找到的。 使用的正确值如下所示:

 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) #define DELTA_EPOCH_IN_MICROSECS 116444736000000000Ui64 // CORRECT #else #define DELTA_EPOCH_IN_MICROSECS 116444736000000000ULL // CORRECT #endif 

PostgreSQL实现gettimeofday for windows:

 /* * gettimeofday.c * Win32 gettimeofday() replacement * * src/port/gettimeofday.c * * Copyright (c) 2003 SRA, Inc. * Copyright (c) 2003 SKC, Inc. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose, without fee, and without a * written agreement is hereby granted, provided that the above * copyright notice and this paragraph and the following two * paragraphs appear in all copies. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */ #include "ch" #include <sys/time.h> /* FILETIME of Jan 1 1970 00:00:00. */ static const unsigned __int64 epoch = ((unsigned __int64) 116444736000000000ULL); /* * timezone information is stored outside the kernel so tzp isn't used anymore. * * Note: this function is not for Win32 high precision timing purpose. See * elapsed_time(). */ int gettimeofday(struct timeval * tp, struct timezone * tzp) { FILETIME file_time; SYSTEMTIME system_time; ULARGE_INTEGER ularge; GetSystemTime(&system_time); SystemTimeToFileTime(&system_time, &file_time); ularge.LowPart = file_time.dwLowDateTime; ularge.HighPart = file_time.dwHighDateTime; tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L); tp->tv_usec = (long) (system_time.wMilliseconds * 1000); return 0; } 

怎么样:

 unsigned long start = GetTickCount(); // stuff that needs to be timed unsigned long delta = GetTickCount() - start; 

GetTickCount()不是很精确,但可能会工作得很好。 如果您看到很多0,16或31毫秒的间隔,请尝试更长的时间间隔或使用更精确的函数,如timeGetTime

我通常做的是这样的:

 unsigned long deltastack; int samples = 0; float average; unsigned long start = GetTickCount(); // stuff that needs to be timed unsigned long delta = GetTickCount() - start; deltastack += delta; if (samples++ == 10) { // total time divided by amount of samples average = (float)deltastack / 10.f; deltastack = 0; samples = 0; } 

在你的情况下,我会使用独立于平台的std :: clock

您可以查看QueryPerformanceCounter和QueryPerformanceFrequency。 这些分辨率非常高 – 在某些硬件定时器上,每十个周期最多可以分辨一次。