Last active
July 12, 2020 13:49
-
-
Save keyou/c4cee15ab90badecdd29ad67c12fca40 to your computer and use it in GitHub Desktop.
A stopwatch accurate to milliseconds.
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
| // Author: keyou | |
| // https://github.com/keyou | |
| // A stopwatch accurate to milliseconds. | |
| // compile command: g++ -std=c++11 -O3 stopwatch.cc -o stopwatch | |
| #include <iostream> | |
| #include <numeric> | |
| #include <chrono> | |
| #include <thread> | |
| #include <iomanip> | |
| int main() { | |
| auto start_time = std::chrono::steady_clock::now(); | |
| while(true) { | |
| auto time = std::chrono::steady_clock::now()-start_time; | |
| auto millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time).count(); | |
| std::cout << std::setfill('0') << std::setw(2) << millseconds/1000/60/60 << ":" | |
| << std::setfill('0') << std::setw(2) << millseconds/1000/60%60 << ":" | |
| << std::setfill('0') << std::setw(2) << millseconds/1000%60 << "." | |
| << std::setfill('0') << std::setw(3) << millseconds%1000 << "\r"; | |
| std::cout.flush(); | |
| std::this_thread::sleep_for(std::chrono::milliseconds(1)); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment