Machine learning algorithms in C++
Timer.hpp
Go to the documentation of this file.
1 //
2 // Created by dodo on 13/12/17.
3 //
4 
5 #ifndef MACHINE_LEARNING_TIMER_HPP
6 #define MACHINE_LEARNING_TIMER_HPP
7 
8 #include <iostream>
9 #include <string>
10 #include <chrono>
11 #include <climits>
12 
13 using namespace std;
14 
18 class Timer {
19  using myClock = chrono::high_resolution_clock;
20  private:
21  unsigned int interval, predictedIters;
22  float lastUpdate;
23  chrono::time_point<chrono::system_clock> startTime;
24 
30  static int numDigits(int32_t x) {
31  if (x == INT_MIN) return 10 + 1;
32  if (x < 0) return numDigits(-x) + 1;
33 
34  if (x >= 10000) {
35  if (x >= 10000000) {
36  if (x >= 100000000) {
37  if (x >= 1000000000)
38  return 10;
39  return 9;
40  }
41  return 8;
42  }
43  if (x >= 100000) {
44  if (x >= 1000000)
45  return 7;
46  return 6;
47  }
48  return 5;
49  }
50  if (x >= 100) {
51  if (x >= 1000)
52  return 4;
53  return 3;
54  }
55  if (x >= 10)
56  return 2;
57  return 1;
58  }
59 
65  static int numDigits(char n) {
66  // if you have the time, replace this with a static initialization to avoid
67  // the initial overhead & unnecessary branch
68  static char x[256] = {0};
69  if (x[0] == 0) {
70  for (char c = 1; c != 0; c++)
71  x[c] = numDigits((int32_t) c);
72  x[0] = 1;
73  }
74  return x[n];
75  }
76 
83  static string zeroPad(int value, unsigned int desiredSize) {
84  return string(desiredSize - numDigits(value), '0').append(to_string(value));
85  }
86 
87  public:
93  explicit Timer(unsigned int interval = 0, unsigned int predictedIters = 0) :
94  interval(interval),
95  predictedIters(predictedIters), lastUpdate(0) {
96  }
97 
101  void start() {
102  this->startTime = myClock::now();
103  }
104 
110  bool activate(unsigned int currentIter = 0) {
111  chrono::time_point<chrono::system_clock> currentTime = myClock::now();
112  float totalSeconds = ((chrono::duration<float>) (currentTime - startTime)).count();
113 
114  if (totalSeconds - lastUpdate > interval) {
115  lastUpdate = totalSeconds;
116 
117  if (currentIter > 0 and predictedIters > 0) {
118  float estimatedTotalSeconds = (totalSeconds / (currentIter)) * predictedIters;
119  string formattedTotalTime = Timer::prettyTime(estimatedTotalSeconds - totalSeconds);
120 
121  cout << "it " << currentIter + 1 << "/" << predictedIters << " (est. " << formattedTotalTime << ")" << endl;
122  }
123  return true;
124  }
125 
126  return false;
127  }
128 
134  static string prettyTime(float secondsFloat) {
135  int totalSeconds = (int) secondsFloat;
136  int milliseconds = (int) (secondsFloat * 1000) % 1000;
137  int seconds = totalSeconds % 60;
138  int minutes = (totalSeconds / 60) % 60;
139  int hours = (totalSeconds / (60 * 60)) % 24;
140 
141  string formattedTime = to_string(hours) + ":" + zeroPad(minutes, 2) + ":"
142  + zeroPad(seconds, 2) + "." + zeroPad(milliseconds, 3);
143  return formattedTime;
144  }
145 
149  string runningTime() {
150  float totalSeconds = ((chrono::duration<float>) (myClock::now() - startTime)).count();
151  return Timer::prettyTime(totalSeconds);
152  }
153 };
154 
155 #endif //MACHINE_LEARNING_TIMER_HPP
float lastUpdate
Definition: Timer.hpp:22
Timer(unsigned int interval=0, unsigned int predictedIters=0)
Creates an instance of a timer object.
Definition: Timer.hpp:93
string runningTime()
Definition: Timer.hpp:149
static int numDigits(char n)
Counts the number of digits in a char.
Definition: Timer.hpp:65
k-nearest neighbors algorithm, able to do regression and classification
static string prettyTime(float secondsFloat)
Formats seconds as HH:MM:SS.mmm.
Definition: Timer.hpp:134
static string zeroPad(int value, unsigned int desiredSize)
Pads a number with 0s on the left until it reaches a desired length.
Definition: Timer.hpp:83
bool activate(unsigned int currentIter=0)
Checks if the time interval passed in the constructor has passed.
Definition: Timer.hpp:110
void start()
Start the timer.
Definition: Timer.hpp:101
unsigned int predictedIters
Definition: Timer.hpp:21
static int numDigits(int32_t x)
Counts the number of digits in an integer.
Definition: Timer.hpp:30
chrono::time_point< chrono::system_clock > startTime
Definition: Timer.hpp:23
A timer that keeps track of time, prints formatted time, checks if an interval has passed etc...
Definition: Timer.hpp:18
chrono::high_resolution_clock myClock
Definition: Timer.hpp:19