Created
March 10, 2015 15:40
-
-
Save sjolsen/bf45bbcbb70f8e8ff37a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
#include <tuple> | |
template <typename Duration = typename std::chrono::steady_clock::duration, typename F> | |
auto time (F&& f) | |
{ | |
using result_type = std::tuple <decltype (std::forward <F> (f) ()), Duration>; | |
auto start = std::chrono::steady_clock::now (); | |
decltype (auto) result = std::forward <F> (f) (); | |
auto stop = std::chrono::steady_clock::now (); | |
auto duration = std::chrono::duration_cast <Duration> (stop - start); | |
return result_type {std::forward <decltype (result)> (result), duration}; | |
} | |
#include <iostream> | |
#include <thread> | |
int foo () | |
{ | |
std::this_thread::sleep_for (std::chrono::seconds {1}); | |
return 42; | |
} | |
void bar () | |
{ | |
std::this_thread::sleep_for (std::chrono::seconds {2}); | |
} | |
int main () | |
{ | |
std::cout << std::get <1> (time <std::chrono::seconds> (foo)).count () << " s\n"; | |
// std::cout << std::get <1> (time <std::chrono::seconds> (bar)).count () << " s\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment