12 int seconds_round = round(seconds);
13 int hours = seconds_round/3600;
14 seconds_round -= 3600*hours;
15 int minutes = seconds_round/60;
16 seconds_round -= 60*minutes;
17 auto fill = stream.fill();
20 stream << hours <<
':' << setw(2) << minutes <<
':' << setw(2) << seconds_round;
22 stream << minutes <<
':' << setw(2) << seconds_round;
34 start_time_(Clock::now()),
35 last_print_(start_time_),
36 iteration_(static_cast<size_t>(-1)),
37 num_iterations_(num_iterations),
38 auto_print_(auto_print),
39 erase_lines_(erase_lines){
43 chrono::duration<double> auto_print,
46 start_time_(Clock::now()),
47 last_print_(start_time_),
48 iteration_(static_cast<size_t>(-1)),
49 num_iterations_(num_iterations),
50 auto_print_(auto_print),
51 erase_lines_(erase_lines){
55 size_t num_iterations,
59 start_time_(Clock::now()),
60 last_print_(start_time_),
61 iteration_(static_cast<size_t>(-1)),
62 num_iterations_(num_iterations),
63 auto_print_(auto_print),
64 erase_lines_(erase_lines){
68 size_t num_iterations,
69 chrono::duration<double> auto_print,
72 start_time_(Clock::now()),
73 last_print_(start_time_),
74 iteration_(static_cast<size_t>(-1)),
75 num_iterations_(num_iterations),
76 auto_print_(auto_print),
77 erase_lines_(erase_lines){
82 if(auto_print_.count() >= 0.
83 && chrono::duration<double>(Clock::now() - last_print_) >= auto_print_){
84 if(erase_lines_) clog <<
"\r\33[2K" << *
this;
85 else clog << *
this <<
'\n';
91 start_time_ = Clock::now();
92 last_print_ = start_time_;
96 num_iterations_ = num_iterations;
98 start_time_ = Clock::now();
99 last_print_ = start_time_;
103 return chrono::duration<double>(Clock::now() - start_time_);
107 if(iteration_ == static_cast<size_t>(-1) || iteration_ == 0){
108 return static_cast<chrono::duration<double>
>(0.);
110 return ElapsedTime()*(num_iterations_ - iteration_)/iteration_;
117 auto eta_raw = chrono::system_clock::now()
118 +
static_cast<chrono::seconds
>(
static_cast<chrono::seconds::rep
>(remaining));
119 auto eta_time = chrono::system_clock::to_time_t(eta_raw);
120 string eta = ctime(&eta_time);
121 if(eta.size() && eta.back() ==
'\n') eta.pop_back();
124 if(timer.
Label() !=
"") stream << timer.
Label() <<
": ";
127 auto precision = stream.precision();
132 stream <<
" (" << 100. <<
"%, ";
135 stream << timer.
Iteration()/elapsed <<
" Hz). ";
137 stream << 0. <<
" Hz). ";
139 stream.precision(precision);
141 stream <<
" left. ETA: " << eta;
152 iteration_ = iteration;
157 return num_iterations_;
161 num_iterations_ = num_iterations;
170 auto_print_ =
static_cast<chrono::duration<double>
>(auto_print);
175 auto_print_ = auto_print;
void PrintTime(ostream &stream, double seconds)
std::chrono::duration< double > AutoPrintTime() const
std::size_t Iteration() const
Timer(std::size_t num_iterations=0, double auto_print=-1., bool erase_lines=false)
std::chrono::duration< double > RemainingTime() const
const std::string & Label() const
std::chrono::duration< double > ElapsedTime() const
std::size_t NumIterations() const
ostream & operator<<(ostream &stream, const Timer &timer)