Created
November 12, 2016 19:52
-
-
Save blippy/69339dcac2e88fb6d8b581d98a5b855a to your computer and use it in GitHub Desktop.
Time sections of code in nanoseconds
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> | |
| class Timer | |
| { | |
| public: | |
| Timer() { start(); }; | |
| void start() {m_start = std::chrono::high_resolution_clock::now(); }; | |
| void stop() {m_stop = std::chrono::high_resolution_clock::now(); }; | |
| double nanos() { std::chrono::duration<double> d = m_stop - m_start; | |
| return std::chrono::duration_cast<std::chrono::nanoseconds>(d).count(); }; | |
| private: | |
| std::chrono::time_point<std::chrono::high_resolution_clock> | |
| m_start, m_stop; | |
| }; | |
| void using_it() | |
| { | |
| Timer tim; | |
| tim.start(); | |
| // ... some code that you want to time | |
| tim.stop(); | |
| cout << "Elapsed: " << tim.nanos() << endl; | |
| // more timing | |
| tim.start(); | |
| // ... yet more code | |
| tim.stop(); | |
| cout << "Elapsed: " << tim.nanos() << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment