MINT2
LoadingBar.cpp
Go to the documentation of this file.
1 #include "Mint/LoadingBar.h"
2 
3 LoadingBar::LoadingBar(int nIterations, int nSteps) :
4  _nIterations(nIterations),
5  _nSteps (nSteps),
6  _prevStep (-1)
7 {
8 
9  _tstart = time(0);
10 
11 
12 }
13 
14 
15 void LoadingBar::printTimeDiff(double minutes){
16 
17  if (minutes > 60.0){
18  int hours = floor( minutes / 60.0 );
19  int mins = floor( minutes - hours*60.0 );
20  std::cout << hours << "h " << mins;
21  }
22  else if (minutes < 60.0 && minutes > 1.0){
23  int mins = floor( minutes );
24  int seconds = floor( double( minutes - mins )*60.0 );
25  std::cout << mins << "m " << seconds;
26  }
27  else{
28  int seconds = floor( (minutes)*600.0 );
29  std::cout << seconds*0.1 << "s";
30  }
31 }
32 
33 void LoadingBar::update(int i){
34 
35  double frac = double(i+1)/double(_nIterations);
36  int steps = floor(_nSteps*frac);
37 
38  int per = floor(100*frac);
39 
40 
41  if (_prevStep != per){
42 
43  double delT = difftime(time(0), _tstart);
44 
45  double detTMins = delT / 60.0;
46  double evtsPerMin = (double(i)/detTMins);
47 
48  double totalTimeMins = double(_nIterations)/evtsPerMin;
49  double remainingTime = totalTimeMins*(1.0-frac);
50 
51  std::cout << "\r [";
52 
53  for (int j = 0; j < _nSteps; j++){
54  if (j < steps) std::cout << "=";
55  else std::cout << " ";
56  }
57 
58  std::cout << "] " << per << "% Complete. ";
59  printTimeDiff(remainingTime);
60  std::cout << " left. " << std::flush;
61 
62  _prevStep = per;
63 
64  }
65 
66  if ( frac == 1.0 ) {
67 
68  double delT = difftime(time(0), _tstart);
69  double detTMins = delT / 60.0;
70  double evtsPerMin = (double(_nIterations)/detTMins);
71 
72  std::cout << "\r Completed in ";
73  printTimeDiff(detTMins);
74  std::cout << " which is " << floor(evtsPerMin) << " iterations/min " << std::flush << std::endl;
75 
76  }
77 }
78 
80 
81 }
int _prevStep
Definition: LoadingBar.h:15
int _nIterations
Definition: LoadingBar.h:13
LoadingBar(int nIterations, int nSteps=20)
Definition: LoadingBar.cpp:3
void printTimeDiff(double minutes)
Definition: LoadingBar.cpp:15
void update(int i)
Definition: LoadingBar.cpp:33
int _nSteps
Definition: LoadingBar.h:14
time_t _tstart
Definition: LoadingBar.h:17