tesseract  5.0.0
cycletimer.h
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 // Portability include to match the Google test environment.
12 
13 #ifndef TESSERACT_UNITTEST_CYCLETIMER_H
14 #define TESSERACT_UNITTEST_CYCLETIMER_H
15 
16 #include <chrono> // for std::chrono
17 
18 // See https://github.com/google/or-tools/blob/master/ortools/base/timer.h
19 class CycleTimer {
20 private:
21  static int64_t now() {
22  return std::chrono::duration_cast<std::chrono::milliseconds>(
23  std::chrono::steady_clock::now().time_since_epoch()).count();
24  }
25 
26 public:
28  Reset();
29  }
30 
31  void Reset() {
32  running_ = false;
33  sum_ = 0;
34  start_ = 0;
35  }
36 
37  // When Start() is called multiple times, only the most recent is used.
38  void Start() {
39  running_ = true;
40  start_ = now();
41  }
42 
43  void Restart() {
44  sum_ = 0;
45  Start();
46  }
47 
48  void Stop() {
49  if (running_) {
50  sum_ += now() - start_;
51  running_ = false;
52  }
53  }
54  int64_t GetInMs() const {
55  return running_ ? now() - start_ + sum_ : sum_;
56  }
57 
58 private:
59  bool running_;
60  int64_t start_;
61  int64_t sum_;
62 };
63 
64 #endif // TESSERACT_UNITTEST_CYCLETIMER_H
void Stop()
Definition: cycletimer.h:48
void Start()
Definition: cycletimer.h:38
void Restart()
Definition: cycletimer.h:43
void Reset()
Definition: cycletimer.h:31
int64_t GetInMs() const
Definition: cycletimer.h:54