Skip to content

Instantly share code, notes, and snippets.

@keyou
Last active July 12, 2020 13:49
Show Gist options
  • Select an option

  • Save keyou/c4cee15ab90badecdd29ad67c12fca40 to your computer and use it in GitHub Desktop.

Select an option

Save keyou/c4cee15ab90badecdd29ad67c12fca40 to your computer and use it in GitHub Desktop.
A stopwatch accurate to milliseconds.
// 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