I like to have the time_ms function defined as such:
// Used to measure intervals and absolute times typedef int64_t msec_t; // Get current time in milliseconds from the Epoch (Unix) // or the time the system started (Windows). msec_t time_ms(void);
The implementation below should work on both Windows and Unix-like systems.
#if defined(__WIN32__) #include <windows.h> msec_t time_ms(void) { return timeGetTime(); } #else #include <sys/time.h> msec_t time_ms(void) { struct timeval tv; gettimeofday(&tv, NULL); return (msec_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; } #endif
Please note that the time returned by the Windows branch is equal to milliseconds from the moment the system starts, and the time returned by the Unix branch from the millisecond since 1970. Thus, if you use this code, rely only on the differences between the moments, and not on the absolute time itself.
Joey adams
source share