我正在编写一个便携式Socket类,支持发送和接收的超时...为了实现这些超时,我正在使用select()
....但是,我有时需要知道我有多久阻止在select()
内部当然在Linux上我将通过在调用gettimeofday()
之前和之后调用select()
然后使用timersub()
来计算delta ...来实现...
鉴于Windows上的select()
接受struct timeval
超时,我应该用什么方法替换Windows上的gettimeofday()?
答案 0 :(得分:20)
我最终找到了这个页面:windows上的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代码项目页面上的this comment找到的。要使用的正确值如下所示:
#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;
}
答案 1 :(得分:4)
怎么样:
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;
}
答案 2 :(得分:1)
在您的情况下,我会使用独立于平台的std::clock
答案 3 :(得分:1)
您可以查看QueryPerformanceCounter和QueryPerformanceFrequency。这些非常高分辨率 - 在某些硬件定时器上每十个周期下降一个滴答。