OR-Tools  9.3
timer.h
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
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//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_TIMER_H_
15#define OR_TOOLS_BASE_TIMER_H_
16
17#include "absl/time/clock.h"
18#include "absl/time/time.h"
21#include "ortools/base/macros.h"
22
23class WallTimer {
24 public:
26 void Reset() {
27 running_ = false;
28 sum_ = 0;
29 }
30 // When Start() is called multiple times, only the most recent is used.
31 void Start() {
32 running_ = true;
33 start_ = absl::GetCurrentTimeNanos();
34 }
35 void Restart() {
36 sum_ = 0;
37 Start();
38 }
39 void Stop() {
40 if (running_) {
41 sum_ += absl::GetCurrentTimeNanos() - start_;
42 running_ = false;
43 }
44 }
45 double Get() const { return GetNanos() * 1e-9; }
46 int64_t GetInMs() const { return GetNanos() / 1000000; }
47 int64_t GetInUsec() const { return GetNanos() / 1000; }
48 inline absl::Duration GetDuration() const {
49 return absl::Nanoseconds(GetNanos());
50 }
51 bool IsRunning() const { return running_; }
52
53 protected:
54 int64_t GetNanos() const {
55 return running_ ? absl::GetCurrentTimeNanos() - start_ + sum_ : sum_;
56 }
57
58 private:
59 bool running_;
60 int64_t start_;
61 int64_t sum_;
62};
63
64// This is meant to measure the actual CPU usage time.
65// TODO(user): implement it properly.
67
68// This is meant to be a ultra-fast interface to the hardware cycle counter,
69// without periodic recalibration, to be even faster than
70// absl::GetCurrentTimeNanos().
71// But this current implementation just uses GetCurrentTimeNanos().
72// TODO(user): implement it.
73class CycleTimer : public WallTimer {
74 public:
75 // This actually returns a number of nanoseconds instead of the number
76 // of CPU cycles.
77 int64_t GetCycles() const { return GetNanos(); }
78};
79
81
82// Conversion routines between CycleTimer::GetCycles and actual times.
84 public:
85 static int64_t SecondsToCycles(double s) {
86 return static_cast<int64_t>(s * 1e9);
87 }
88 static double CyclesToSeconds(int64_t c) { return c * 1e-9; }
89 static int64_t CyclesToMs(int64_t c) { return c / 1000000; }
90 static int64_t CyclesToUsec(int64_t c) { return c / 1000; }
91};
93
94// A WallTimer clone meant to support SetClock(), for unit testing. But for now
95// we just use WallTimer directly.
97
99 public:
100 // We do not own the pointer. The pointer must be valid for the duration
101 // of the existence of the ScopedWallTime instance. Not thread safe for
102 // aggregate_time.
103 explicit ScopedWallTime(double* aggregate_time);
105
106 private:
107 double* aggregate_time_;
108
109 // When the instance was created.
110 WallTimer timer_;
111
112 DISALLOW_COPY_AND_ASSIGN(ScopedWallTime);
113};
114#endif // OR_TOOLS_BASE_TIMER_H_
static int64_t CyclesToUsec(int64_t c)
Definition: timer.h:90
static double CyclesToSeconds(int64_t c)
Definition: timer.h:88
static int64_t CyclesToMs(int64_t c)
Definition: timer.h:89
static int64_t SecondsToCycles(double s)
Definition: timer.h:85
int64_t GetCycles() const
Definition: timer.h:77
~ScopedWallTime()
Definition: timer.cc:22
ScopedWallTime(double *aggregate_time)
Definition: timer.cc:16
void Start()
Definition: timer.h:31
void Stop()
Definition: timer.h:39
int64_t GetInUsec() const
Definition: timer.h:47
void Reset()
Definition: timer.h:26
WallTimer()
Definition: timer.h:25
absl::Duration GetDuration() const
Definition: timer.h:48
void Restart()
Definition: timer.h:35
int64_t GetInMs() const
Definition: timer.h:46
bool IsRunning() const
Definition: timer.h:51
double Get() const
Definition: timer.h:45
int64_t GetNanos() const
Definition: timer.h:54
WallTimer UserTimer
Definition: timer.h:66
CycleTimerBase CycleTimerInstance
Definition: timer.h:92
WallTimer ClockTimer
Definition: timer.h:96
CycleTimer SimpleCycleTimer
Definition: timer.h:80