From 76788013d064ed7f382df8013062052c8a60a86e Mon Sep 17 00:00:00 2001 From: Forrest Green Date: Fri, 7 Apr 2017 11:28:22 -0700 Subject: [PATCH] Add macro for doing quick and dirty benchmarks by printing out execution time of functions --- geode/utility/CMakeLists.txt | 1 + geode/utility/timed_call.h | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 geode/utility/timed_call.h diff --git a/geode/utility/CMakeLists.txt b/geode/utility/CMakeLists.txt index bbabbd6b..d274c353 100644 --- a/geode/utility/CMakeLists.txt +++ b/geode/utility/CMakeLists.txt @@ -68,6 +68,7 @@ set(module_HEADERS stream.h str.h time.h + timed_call.h tr1.h type_traits.h Unique.h diff --git a/geode/utility/timed_call.h b/geode/utility/timed_call.h new file mode 100644 index 00000000..b6575774 --- /dev/null +++ b/geode/utility/timed_call.h @@ -0,0 +1,25 @@ +#pragma once +#include +namespace geode { + +struct TimedCallHelper { + std::string msg; + real start_time = get_time(); + TimedCallHelper(const string& new_msg) + : msg(new_msg) + { } + ~TimedCallHelper() { + const auto end_time = get_time(); + std::cout << msg << " took " << (end_time - start_time) << " seconds.\n"; + } +}; + +template auto timed_call(const string& msg, Fn&& fn, Args&&... args) -> decltype(fn(std::forward(args)...)) { + GEODE_UNUSED const auto& helper = TimedCallHelper{msg}; + return fn(std::forward(args)...); +} + +// Call a function printing execution time to cout +#define GEODE_TIMED_CALL(fn,...) geode::timed_call(#fn,fn,__VA_ARGS__) + +} // geode namespace \ No newline at end of file