c - using gettimeofday() equivalents on windows -


i'm trying use 2 different equivalents unix's gettimeofday() function on windows, using visual studio 2013.

i took first 1 here. second one, i'm using _ftime64_s function, explained here.

they work, not expected. want different values when printing seconds, or @ least milliseconds, same value printings gettimeofday() (mytime1 & mytime2) , _ftime64_s (mytime3 & mytime4).

however, worth mentioning value of milliseconds indeed different between these 2 functions (that is, milliseconds value of mytime1/mytime2 different mytime3/mytime4).

here's code:

#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: broken versions have 8 trailing zero's, 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 struct timeval , 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 _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); } 

i tried other format specifiers (%f, %lu) , castings ((float), (double), (long), (size_t)), didn't matter. suggestions welcomed.

queryperformancecounter used accurate timing on windows. usage can follows:

uint64_t microseconds() {     large_integer fq, t;     queryperformancefrequency(&fq);     queryperformancecounter(&t);     return (1000000 * t.quadpart) / fq.quadpart; } 

this not work epoch far know. need getsystemtimepreciseasfiletime available on windows 8 , higher.

uint64_t mygetsystemtimepreciseasfiletime() {     hmodule lib = loadlibraryw(l"kernel32.dll");     if (!lib) return 0;     farproc fp = getprocaddress(lib, "getsystemtimepreciseasfiletime");     ularge_integer largeint;     largeint.quadpart = 0;     if (fp)     {         t_getsystemtimepreciseasfiletime* pfn = (t_getsystemtimepreciseasfiletime*)fp;         filetime filetime = { 0 };         pfn(&filetime);         largeint.highpart = filetime.dwhighdatetime;         largeint.lowpart = filetime.dwlowdatetime;     }     freelibrary(lib);     return largeint.quadpart; }  int main() {     uint64_t t1 = microseconds();     uint64_t t2 = microseconds();     printf("t1: %llu\n", t1);     printf("t2: %llu\n", t2);     return (0); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -