Timing in an elegant way in c++

后端 未结 4 475
难免孤独
难免孤独 2021-01-03 01:31

I am interested in timing the execution time of a free function or a member function (template or not). Call TheFunc the function in question, its call being



        
4条回答
  •  礼貌的吻别
    2021-01-03 01:59

    With variadic template, you may do:

    template 
    double Time_function(F&& f, Ts&&...args)
    {
        std::clock_t start = std::clock();
        std::forward(f)(std::forward(args)...);
        return static_cast(std::clock() - start) / static_cast(CLOCKS_PER_SEC);
    }
    

提交回复
热议问题