我正在编写一个支持发送和接收超时的可移植 Socket 类...为了实现我正在使用的这些超时select()
...但是,我有时需要知道我select()
在 Linux 上被阻塞了多长时间将通过gettimeofday()
在我调用之前和之后调用select()
然后使用timersub()
来计算增量来实现...
鉴于select()
在 Windows 上接受struct timeval
超时,我应该使用什么方法来替换 Windows 上的 gettimeofday()?
我正在编写一个支持发送和接收超时的可移植 Socket 类...为了实现我正在使用的这些超时select()
...但是,我有时需要知道我select()
在 Linux 上被阻塞了多长时间将通过gettimeofday()
在我调用之前和之后调用select()
然后使用timersub()
来计算增量来实现...
鉴于select()
在 Windows 上接受struct timeval
超时,我应该使用什么方法来替换 Windows 上的 gettimeofday()?
我最终找到了这个页面:gettimeofday()
function for Windows(现在通过 Wayback Machine),它gettimeofday()
在 Windows 上有一个方便的、花哨的实现。它使用该GetSystemTimeAsFileTime()
方法获得准确的时钟。
更新:这是“Unix 到 HPC 的 Windows 移植字典” gettimeofday()
(现在通过 Wayback Machine)的另一个活动链接,它指向 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 在 Windows 上的 gettimeofday 实现:
/*
* 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 "c.h"
#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。这些分辨率非常高——在某些硬件定时器上低至每十个周期一个滴答声。