Does anyone know an equivalent function of the gettimeofday()
function in Windows environment? I am comparing a code execution time in Linux vs Windows. I am us
GetLocalTime()
for the time in the system timezone, GetSystemTime()
for UTC. If you want a seconds-since-epoch time, use SystemTimeToFileTime()
or GetSystemTimeAsFileTime()
.
For interval taking, use GetTickCount()
. It returns milliseconds since startup.
For taking intervals with the best possible resolution (limited by hardware only), use QueryPerformanceCounter()
.
Nowadys I would use the following for gettimeofday() on Windows, which is using GetSystemTimePreciseAsFileTime() if compiled for Windows 8 or higher and GetSystemTimeAsFileTime() otherwise:
#include <Windows.h>
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
if (tv) {
FILETIME filetime; /* 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 00:00 UTC */
ULARGE_INTEGER x;
ULONGLONG usec;
static const ULONGLONG epoch_offset_us = 11644473600000000ULL; /* microseconds betweeen Jan 1,1601 and Jan 1,1970 */
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
GetSystemTimePreciseAsFileTime(&filetime);
#else
GetSystemTimeAsFileTime(&filetime);
#endif
x.LowPart = filetime.dwLowDateTime;
x.HighPart = filetime.dwHighDateTime;
usec = x.QuadPart / 10 - epoch_offset_us;
tv->tv_sec = (time_t)(usec / 1000000ULL);
tv->tv_usec = (long)(usec % 1000000ULL);
}
if (tz) {
TIME_ZONE_INFORMATION timezone;
GetTimeZoneInformation(&timezone);
tz->tz_minuteswest = timezone.Bias;
tz->tz_dsttime = 0;
}
return 0;
}
Here is a free implementation:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdint.h> // portable: uint64_t MSVC: __int64
// MSVC defines this in winsock2.h!?
typedef struct timeval {
long tv_sec;
long tv_usec;
} timeval;
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
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
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;
}
If you really want a Windows gettimeofday() implementation, here is one from PostgreSQL that uses Windows APIs and the proper conversions.
However if you want to time code, I suggest you look into QueryPerformanceCounter() or by directly invoking the TSC if you're only going to run on x86 for example.
This is the version of c++11 that uses chrono.
Thank you, Howard Hinnant for advice.
#if defined(_WIN32)
#include <chrono>
int gettimeofday(struct timeval* tp, struct timezone* tzp) {
namespace sc = std::chrono;
sc::system_clock::duration d = sc::system_clock::now().time_since_epoch();
sc::seconds s = sc::duration_cast<sc::seconds>(d);
tp->tv_sec = s.count();
tp->tv_usec = sc::duration_cast<sc::microseconds>(d - s).count();
return 0;
}
#endif // _WIN32