Last active
June 15, 2016 02:08
-
-
Save MehdiNS/9a8e6d892763c3f480ee3b6cd6996a2b to your computer and use it in GitHub Desktop.
Generic function timer - could be more fancy but it is good enough
This file contains 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
// -First idea was to do it thanks to RAII. Problem is it requires a lot of brackets | |
// -Then i wanted to go full variadic generic template function, | |
// but i got stuck because i would have to pass a variadic function | |
// to a function which doesn't make any sense, and was overkill | |
// - This solution seems the best for now... Client code is okay-ish... | |
#include <iostream> | |
#include <chrono> | |
#include <thread> | |
#include <string> | |
#include <utility> | |
using namespace std; | |
using namespace std::chrono; | |
template <class F, class ... Args> | |
auto genericTimer(F f, Args && ... args) | |
{ | |
auto avant = system_clock::now(); | |
f(forward<Args>(args)...); | |
auto apres = system_clock::now(); | |
return 0.001f * duration_cast<microseconds>(apres-avant).count(); | |
} | |
void foo(int, string){ std::this_thread::sleep_for(std::chrono::milliseconds(16));} | |
int main() | |
{ | |
auto res = genericTimer(foo, 42, "Scene"); | |
cout << "Function time : " << res << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment