Last active
January 16, 2017 01:32
-
-
Save benloong/c31fe542d598bbc71c4a52b990e97bbf to your computer and use it in GitHub Desktop.
C++17 generic callable profiling.
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 "timed_invoke.h" | |
int* func0(int x, int y, const char* z) { | |
return nullptr; | |
} | |
void func1(int x, const char* z) { | |
return; | |
} | |
void func2() { | |
return; | |
} | |
template<typename T> | |
void func3(T x) { | |
return; | |
} | |
struct S { | |
int member_func(int x, float y) { | |
return x; | |
} | |
bool operator()() { | |
return false; | |
} | |
void operator()(int x) { | |
return; | |
} | |
}; | |
int main() { | |
int* ret = timed_invoke(func0, 1, 2, "hello"); | |
timed_invoke(func1, 1, "hello"); | |
timed_invoke(func2); | |
timed_invoke(func3<int>, 2); // template function | |
auto h = timed_invoke([]{return "hello"; }); // lambda | |
S s; | |
int r = timed_invoke(&S::member_func, s, 1, 2.0f); // member function | |
bool r0 = timed_invoke(s); // bool S::operator()() | |
timed_invoke(s, 10); // void S::operator()(int) | |
} |
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<iostream> | |
#include<chrono> | |
#include<type_traits> | |
#include<functional> | |
using namespace std::chrono; | |
template <typename Callable, typename ...Args> | |
decltype(auto) timed_invoke(Callable&& callable, Args&& ...args) | |
{ | |
struct timer{ | |
timer(){ | |
t0 = high_resolution_clock::now(); | |
} | |
~timer(){ | |
auto t1 = high_resolution_clock::now(); | |
std::cout << (t1 - t0).count() << "nanoseconds.\n"; | |
} | |
time_point<high_resolution_clock> t0; | |
} t; | |
return std::invoke(callable, std::forward<Args>(args)...); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment