Easily measure elapsed time

前端 未结 26 2187
栀梦
栀梦 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:13

    I usually use the following:

    #include 
    #include 
    
    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;
    
    template
    floating_seconds run_test(Func&& func, Args&&... args)
    {
       const auto t0 = perf_clock::now();
       std::forward(func)(std::forward(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.

提交回复
热议问题