fastglmm
Massively scalable generalized linear mixed models
Loading...
Searching...
No Matches
progressbar.h
Go to the documentation of this file.
1
2
3#ifndef _PROGRESS_BAR_
4#define _PROGRESS_BAR_
5
6#include <iostream>
7#include <chrono>
8#include <thread> // For std::this_thread::sleep_for
9
10
12
13 public:
14
15 ProgressBar( const int &total ):
16 total(total) {
17 startTime = std::chrono::high_resolution_clock::now();
18 }
19
20 // Update with number of completed tasks and out-stream
21 void update( const int &progress, ostream &strm);
22
23 private:
24 std::chrono::high_resolution_clock::time_point startTime;
25 int total;
26 int barWidth = 50; // Width of the progress bar in characters
27};
28
29
30void ProgressBar::update( const int &progress, ostream &strm){
31
32 // Calculate progress percentage
33 double percentage = static_cast<double>(progress) / total;
34 int filledWidth = static_cast<int>(barWidth * percentage);
35
36 // Print the bar
37 strm << "\r["; // \r returns cursor to beginning of line
38 for (int i = 0; i < filledWidth; ++i) {
39 strm << "=";
40 }
41 for (int i = filledWidth; i < barWidth; ++i) {
42 strm << " ";
43 }
44 strm << "] " << static_cast<int>(percentage * 100.0) << "%";
45
46 if( progress > 0 ){
47 // Calculate ETA
48 auto currentTime = std::chrono::high_resolution_clock::now();
49 auto elapsedTime = std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count();
50
51 int minutes, seconds;
52
53 if( progress == total){
54 minutes = static_cast<int>(elapsedTime / 60);
55 seconds = static_cast<int>(elapsedTime) % 60;
56 }else{
57 double estimatedTotalTime = (elapsedTime / percentage);
58 double etaSeconds = estimatedTotalTime - elapsedTime;
59
60 minutes = static_cast<int>(etaSeconds / 60);
61 seconds = static_cast<int>(etaSeconds) % 60;
62 }
63 strm << " ETA: " << minutes << "m " << seconds << "s ";
64 strm << std::flush; // Ensure immediate output
65 }
66}
67
68
69
70
71#endif
ProgressBar(const int &total)
Definition progressbar.h:15
void update(const int &progress, ostream &strm)
Definition progressbar.h:30