Easily measure elapsed time

前端 未结 26 2138
栀梦
栀梦 2020-11-22 04:13

I am trying to use time() to measure various points of my program.

What I don\'t understand is why the values in the before and after are the same? I understand thi

相关标签:
26条回答
  • 2020-11-22 05:12

    The values printed by your second program are seconds, and microseconds.

    0 26339 = 0.026'339 s =   26339 µs
    4 45025 = 4.045'025 s = 4045025 µs
    
    0 讨论(0)
  • 2020-11-22 05:13

    I usually use the following:

    #include <chrono>
    #include <type_traits>
    
    using perf_clock = std::conditional<
        std::chrono::high_resolution_clock::is_steady,
        std::chrono::high_resolution_clock,
        std::chrono::steady_clock
    >::type;
    
    using floating_seconds = std::chrono::duration<double>;
    
    template<class F, class... Args>
    floating_seconds run_test(Func&& func, Args&&... args)
    {
       const auto t0 = perf_clock::now();
       std::forward<Func>(func)(std::forward<Args>(args)...);
       return floating_seconds(perf_clock::now() - t0);
    } 
    

    It's the same as @nikos-athanasiou proposed except that I avoid using of a non-steady clock and use floating number of seconds as a duration.

    0 讨论(0)
提交回复
热议问题