Last active
November 10, 2019 09:53
-
-
Save byBretema/f2e2a71f5e18cb699d8c315e127a56f2 to your computer and use it in GitHub Desktop.
For easy benchmarking - Timer object that show time when is destroyed.
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> | |
// ------------------------------------------ | |
// 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