Last active
November 21, 2020 12:09
-
-
Save weigert/9dec1a41326cd3c917b23681654dd793 to your computer and use it in GitHub Desktop.
C++ Timer Header - Benchmarking + Timed Thread Execution
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
#include <thread> | |
#include <chrono> | |
#include <atomic> | |
namespace timer{ | |
//Benchmarking Helper | |
template<typename D, typename F, typename... Args> | |
void benchmark(F function, Args&&... args){ | |
auto start = std::chrono::high_resolution_clock::now(); | |
function(args...); | |
auto stop = std::chrono::high_resolution_clock::now(); | |
auto duration = std::chrono::duration_cast<D>(stop - start); | |
std::cout<<"Execution Time: "<<duration.count()<<std::endl; | |
}; | |
//Timed Execution Helper | |
template<typename D> | |
class Timer{ | |
//Finish the Guy | |
std::atomic<bool> active = false; | |
//Should keep track of execution time... | |
std::chrono::time_point<std::chrono::high_resolution_clock> curTime; | |
D delayTime = D(0); | |
//Thread for the guy | |
std::thread t; | |
public: | |
//Timeout Setter | |
template<typename F> | |
void set_timeout(D duration, F function){ | |
active = true; | |
t = std::thread([=](){ | |
std::this_thread::sleep_for(duration); | |
if(!active) return; //Maybe the Thread was terminated before the execution of function | |
function(); | |
return; | |
}); | |
t.detach(); | |
}; | |
//Interval Setter | |
template<typename F, typename... Args> | |
void set_interval(D* duration, F function, Args&&... args){ | |
active = true; | |
t = std::thread([=](){ | |
while(active){ | |
std::this_thread::sleep_for(*duration); | |
function(args...); | |
} | |
return; | |
}); | |
t.detach(); | |
}; | |
//Interval Setter | |
template<typename F, typename... Args> | |
void set_const_interval(D* duration, F function, Args&&... args){ | |
active = true; | |
t = std::thread([=](){ | |
while(active){ | |
std::this_thread::sleep_for(*duration-delayTime); | |
curTime = std::chrono::high_resolution_clock::now(); | |
function(args...); | |
delayTime = std::chrono::duration_cast<D>(std::chrono::high_resolution_clock::now() - curTime); | |
} | |
return; | |
}); | |
t.detach(); | |
}; | |
//Stopper | |
void stop(){ | |
//Stop the Process and Join the Thread | |
active = false; | |
if(t.joinable()){ | |
t.join(); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment