// // Helper to measure time taken by functions // // Code taken from https://github.com/picanumber/bureaucrat/blob/master/time_lapse.h // #ifndef MEASURE_HPP #define MEASURE_HPP #include #include #include /* * @brief Macro to simplify the benchmark() function usage. * - more concise * - already fill the unit type * * @param name (String) representing the name of the function (for the log) * @param repeat (Int) number of times to call the function for the time average * @param fct (F type) function to call * @param ... (Args type) arguments of the function to call * * Usage examples: * - without macro: * measure::benchmark("ms", "medianBlur", repeat, medianBlur, src, dst, 9); * - with macro: * BENCHMARK_MS("medianBlur", repeat, medianBlur, src, dst, 9); */ #define BENCHMARK_NS(name, repeat, fct, ...) measure::benchmark("ns", name, repeat, fct, __VA_ARGS__) #define BENCHMARK_US(name, repeat, fct, ...) measure::benchmark("us", name, repeat, fct, __VA_ARGS__) #define BENCHMARK_MS(name, repeat, fct, ...) measure::benchmark("ms", name, repeat, fct, __VA_ARGS__) #define BENCHMARK_S(name, repeat, fct, ...) measure::benchmark("s", name, repeat, fct, __VA_ARGS__) #define fw(what) std::forward(what) /** * @ class measure * @ brief Class to measure the execution time of a callable */ template < typename TimeT = std::chrono::milliseconds, class ClockT = std::chrono::system_clock > struct measure { /** * @ fn execution * @ brief Returns the quantity (count) of the elapsed time as TimeT units */ template static typename TimeT::rep execution(F&& func, Args&&... args) { auto start = ClockT::now(); fw(func)(std::forward(args)...); auto duration = std::chrono::duration_cast(ClockT::now() - start); return duration.count(); } /** * Function that executes the function 'repeat' times, measure the average time taken and logs on the console * the time. * @tparam F * @tparam Args * @param unit String representing the time unit (for the log). Can be either 's', 'ms', 'us' or 'ns' * @param repeat Number of times to do the measure * @param func Function to benchmark * @param args Arguments of the function 'func' */ template static void benchmark(const std::string &unit, const std::string &name, int repeat, F&& func, Args&&... args) { auto avg = duration(func, (args)...); for(int i = 0; i < repeat-1; i++) { avg += duration(func, (args)...); } std::cout << name << "\t " << (avg / repeat).count() << " [" << unit << "]" << std::endl; } /** * @ fn duration * @ brief Returns the duration (in chrono's type system) of the elapsed time */ template static TimeT duration(F&& func, Args&&... args) { auto start = ClockT::now(); fw(func)(std::forward(args)...); return std::chrono::duration_cast(ClockT::now() - start); } }; #undef fw #endif // MEASURE_HPP