Alternatives to

后端 未结 2 1610
死守一世寂寞
死守一世寂寞 2021-02-06 09:49

I\'m looking for a C++11 version of the library. Were any like this included with C++11?

EDIT: Anything with more functionality would be perfe

2条回答
  •  庸人自扰
    2021-02-06 10:19

    C++11 includes the header, which provides different types of clocks (I'll use the high resolution one), that have a now function. You can subtract two of these times received from now() to get the total number of s (I'll use seconds) between them:

    using clock = std::chrono::high_resolution_clock;
    using unit = std::chrono::seconds;
    std::chrono::time_point startTime = clock::now(); //get starting time
    
    ... //do whatever stuff you have to do
    
    std::chrono::time_point thisTime = clock::now();
    long long secondsElapsed = std::chrono::duration_cast(thisTime - startTime).count();
    
    //now use secondsElapsed however you wish
    //you can also use other units, such as milliseconds or nanoseconds
    

    Do note, however, that secondsElapsed is not guaranteed to be positive unless the is_steady member of the clock is true because that member being true means that a subsequent call to now() will give a larger number than a former call to now().

提交回复
热议问题