tesseract  5.0.0-alpha-619-ge9db
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 "absl/time/clock.h" // for GetCurrentTimeNanos
17 
18 // See https://github.com/google/or-tools/blob/master/ortools/base/timer.h
19 class CycleTimer {
20 public:
22  Reset();
23  }
24 
25  void Reset() {
26  running_ = false;
27  sum_ = 0;
28  start_ = 0;
29  }
30 
31  // When Start() is called multiple times, only the most recent is used.
32  void Start() {
33  running_ = true;
34  start_ = absl::GetCurrentTimeNanos();
35  }
36 
37  void Restart() {
38  sum_ = 0;
39  Start();
40  }
41 
42  void Stop() {
43  if (running_) {
44  sum_ += absl::GetCurrentTimeNanos() - start_;
45  running_ = false;
46  }
47  }
48  int64_t GetInMs() const { return GetNanos() / 1000000; }
49 
50  protected:
51  int64_t GetNanos() const {
52  return running_ ? absl::GetCurrentTimeNanos() - start_ + sum_ : sum_;
53  }
54 
55  private:
56  bool running_;
57  int64_t start_;
58  int64_t sum_;
59 };
60 
61 #endif // TESSERACT_UNITTEST_CYCLETIMER_H
CycleTimer::Start
void Start()
Definition: cycletimer.h:32
CycleTimer::CycleTimer
CycleTimer()
Definition: cycletimer.h:21
CycleTimer::GetInMs
int64_t GetInMs() const
Definition: cycletimer.h:48
CycleTimer::GetNanos
int64_t GetNanos() const
Definition: cycletimer.h:51
CycleTimer::Stop
void Stop()
Definition: cycletimer.h:42
CycleTimer::Reset
void Reset()
Definition: cycletimer.h:25
CycleTimer
Definition: cycletimer.h:19
CycleTimer::Restart
void Restart()
Definition: cycletimer.h:37