【发布时间】:2023-03-11 02:32:01
【问题描述】:
我正在尝试使用 Visual Studio 2013 在 Windows 上为 UNIX 的 gettimeofday() 函数使用 2 个不同的等效项。
我从here 拿了第一个。作为第二个,我正在使用 _ftime64_s 函数,正如here 解释的那样。
他们工作,但不像我预期的那样。我想在打印秒数时获得不同的值,或者至少是毫秒,但我使用 gettimeofday() (mytime1 & mytime2) 和 _ftime64_s (mytime3 & mytime4) 得到相同的值。
但值得一提的是,这两个函数的毫秒值确实不同(即mytime1/mytime2和mytime3/mytime4的毫秒值不同)。
这是我的代码:
#include <stdio.h>
#include <Windows.h>
#include <stdint.h>
#include <sys/timeb.h>
#include <time.h>
#define WIN32_LEAN_AND_MEAN
int gettimeofday(struct timeval * tp, struct timezone * tzp)
{
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
time = ((uint64_t)file_time.dwLowDateTime);
time += ((uint64_t)file_time.dwHighDateTime) << 32;
tp->tv_sec = (long)((time - EPOCH) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
return 0;
}
int main()
{
/* working with struct timeval and gettimeofday equivalent */
struct timeval mytime1;
struct timeval mytime2;
gettimeofday(&(mytime1), NULL);
gettimeofday(&(mytime2), NULL);
printf("Seconds: %d\n", (int)(mytime1.tv_sec));
printf("Milliseconds: %d\n", (int)(mytime1.tv_usec));
printf("Seconds: %d\n", (int)(mytime2.tv_sec));
printf("Milliseconds: %d\n", (int)(mytime2.tv_usec));
/* working with _ftime64_s */
struct _timeb mytime3;
struct _timeb mytime4;
_ftime64_s(&mytime3);
_ftime64_s(&mytime4);
printf("Seconds: %d\n", mytime3.time);
printf("Milliseconds: %d\n", mytime3.millitm);
printf("Seconds: %d\n", mytime4.time);
printf("Milliseconds: %d\n", mytime4.millitm);
return (0);
}
我尝试了其他格式说明符 (%f, %lu) 和铸件 ((float), (double), (long), (size_t)),但没关系。欢迎提出建议。
【问题讨论】:
-
struct timeval的tv_usec成员是 micro 秒,而不是像struct _timeb或struct __timeb64的millitm成员那样的毫秒...所以你需要除以 1000 得到毫秒。另外,您不应该将struct __timeb64与_ftime64_s或struct _timeb与_ftime_s一起使用吗? -
您似乎在问为什么一个函数在两个不同的操作系统上花费的时间不同。这都是见仁见智的问题。
-
另外,当你调用函数如此接近时,为什么你会期望时间会有所不同?您的整个程序可能在一秒钟内完成,因此如果所有四个调用在大多数时间都以秒(甚至可能是毫秒)为单位给出相同的时间也就不足为奇了。如果您想要不同的时间,请在通话之间添加延迟。