Last active
August 29, 2015 14:22
-
-
Save 2bbb/3f6467d5c47c2144beb8 to your computer and use it in GitHub Desktop.
SimpleStopWatch
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
| namespace laziness { | |
| struct SimpleStopWatch { | |
| void start() { | |
| _start = std::chrono::high_resolution_clock::now(); | |
| } | |
| template <typename T = std::chrono::nanoseconds> | |
| typename T::rep rap() { | |
| _end = std::chrono::high_resolution_clock::now(); | |
| return std::chrono::duration_cast<T>(_end - _start).count(); | |
| } | |
| template <typename T = std::chrono::nanoseconds> | |
| typename T::rep getLastRap() const { | |
| return std::chrono::duration_cast<T>(_end - _start).count(); | |
| } | |
| auto getLastRapMilliseconds() const | |
| -> decltype(getLastRap<std::chrono::milliseconds>()) { | |
| return getLastRap<std::chrono::milliseconds>(); | |
| } | |
| auto getLastRapMicroseconds() const | |
| -> decltype(getLastRap<std::chrono::microseconds>()) { | |
| return getLastRap<std::chrono::microseconds>(); | |
| } | |
| auto getLastRapNanoseconds() const | |
| -> decltype(getLastRap<std::chrono::nanoseconds>()) { | |
| return getLastRap<std::chrono::nanoseconds>(); | |
| } | |
| decltype(std::chrono::high_resolution_clock::now()) _start, _end; | |
| }; | |
| }; |
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> | |
| int main(int argc, char *argv[]) { | |
| laziness::SimpleStopWatch w; | |
| w.start(); | |
| for(int i = 0; i < 100; i++) { | |
| std::cout << w.rap() << std::endl; | |
| } | |
| std::cout << "last" << std::endl; | |
| std::cout << w.getLastRap() << std::endl; | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment