我应该用什么来替换 Windows 上的 gettimeofday() ?

发布于 2024-08-10 05:38:03 字数 361 浏览 2 评论 0 原文

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

鉴于 Windows 上的 select() 接受 struct timeval 作为超时,应该使用什么方法我曾经在 Windows 上替换 gettimeofday() ?

I'm writing a portable Socket class that supports timeouts for both sending and receiving... To implement these timeouts I'm using select().... But, I sometimes need to know how long I was blocked inside select() which of course on Linux I would implement by calling gettimeofday() before and after I call select() and then using timersub() to calculate the delta...

Given that select() on Windows accepts struct timeval for it's timeout, what method should I used to replace gettimeofday() on Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罗罗贝儿 2024-08-17 05:38:03

我最终找到了这个页面: gettimeofday() Windows 函数(现在通过 Wayback Machine),它有一个方便、花哨的 gettimeofday() 实现视窗。它使用 GetSystemTimeAsFileTime() 方法来获取准确的时钟。

更新:这是来自“Unix to Windows Porting Dictionary for HPC”的替代活动链接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 纳秒间隔的数量)。此拼写错误是通过此评论发现的谷歌代码项目页面。使用的正确值如下所示:

#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;
}

I ended up finding this page: gettimeofday() function for Windows (now via the Wayback Machine) which has a handy, dandy implementation of gettimeofday() on Windows. It uses the GetSystemTimeAsFileTime() method to get an accurate clock.

Update: Here's an alternative active link from the 'Unix to Windows Porting Dictionary for HPC' gettimeofday() (now via the Wayback Machine) that points to the implementation the OP referred to. Note also that there's a typo in the linked implementation:

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

The values shown are missing an extra 0 at the end (they assumed microseconds, not the number of 100-nanosecond intervals). This typo was found via this comment on a Google code project page. The correct values to use are shown below:

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

PostgreSQL's implementation of 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 "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;
}
世界如花海般美丽 2024-08-17 05:38:03

怎么样:

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;  
}

How about:

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

GetTickCount() is not very precise, but will probably work well. If you see a lot of 0, 16 or 31 millisecond intervals, try timing over longer intervals or use a more precise function like timeGetTime.

What I usually do is this:

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;  
}
捂风挽笑 2024-08-17 05:38:03

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

In your case I would use the platform independent std::clock

时光瘦了 2024-08-17 05:38:03

您可以查看 QueryPerformanceCounter 和 QueryPerformanceFrequency。这些分辨率非常 - 在某些硬件定时器上低至每十个周期一次。

You can check out QueryPerformanceCounter and QueryPerformanceFrequency. These are very high resolution- down to one tick per ten cycles on some hardware- timers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文