Measuring time with a resolution of microseconds in C++?

后端 未结 6 1452
醉话见心
醉话见心 2020-12-11 22:02

I\'m looking for a way to measure microsecs in C++/Windows.

I read about the \"clock\" function, but it returns only milliseconds...
Is there a way to do it?

6条回答
  •  死守一世寂寞
    2020-12-11 22:49

    (since no-one has mentioned a pure c++ approach yet),

    as of c++11:

    #include 
    std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch())
    

    gets you the number of microseconds since 1970-01-01, and a port of php's microtime(true) api would be

    #include 
    double microtime(){
        return (double(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count()) / double(1000000));
    }
    

    gets you the number of seconds since 1970-01-01 with microsecond precision

提交回复
热议问题