Skip to content

Instantly share code, notes, and snippets.

@byBretema
Last active November 10, 2019 09:53
Show Gist options
  • Save byBretema/f2e2a71f5e18cb699d8c315e127a56f2 to your computer and use it in GitHub Desktop.
Save byBretema/f2e2a71f5e18cb699d8c315e127a56f2 to your computer and use it in GitHub Desktop.
For easy benchmarking - Timer object that show time when is destroyed.
#include <chrono>
// ------------------------------------------
// Timer
// ------------------------------------------
struct Timer {
using clock = std::chrono::high_resolution_clock;
using unit = std::chrono::duration<double, std::nano>;
private:
std::string_view msg;
std::chrono::time_point<clock> ini;
public:
Timer(std::string_view msg = ""): msg(msg), ini(clock::now()) {}
~Timer(){
std::cout << "[TIMER] - " << msg << ((msg == "")? "":" : ")
<< unit(clock::now() - ini).count() << " ns" << "\n";
}
};
// ------------------------------------------
/*
USAGE EXAMPLE:
switch (input) {
case 0:{
Timer t;
// <first task>
}break;
case 1:{
Timer t("[1]");
// <second task>
}break;
default:{
Timer t("[Fail]");
std::cout << "[!] - Undefined input!" << "\n";
}break;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment