Equivalent of gettimeday() for Windows

前端 未结 5 1456
星月不相逢
星月不相逢 2020-12-23 17:43

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

5条回答
  •  长情又很酷
    2020-12-23 18:42

    This is the version of c++11 that uses chrono.

    Thank you, Howard Hinnant for advice.

    #if defined(_WIN32)
    #include 
    
    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(d);
      tp->tv_sec = s.count();
      tp->tv_usec = sc::duration_cast(d - s).count();
    
      return 0;
    }
    
    #endif // _WIN32
    

提交回复
热议问题