OR-Tools  9.1
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
52 protected:
53 int64_t GetNanos() const {
54 return running_ ? absl::GetCurrentTimeNanos() - start_ + sum_ : sum_;
55 }
56
57 private:
58 bool running_;
59 int64_t start_;
60 int64_t sum_;
61};
62
63// This is meant to measure the actual CPU usage time.
64// TODO(user): implement it properly.
66
67// This is meant to be a ultra-fast interface to the hardware cycle counter,
68// without periodic recalibration, to be even faster than
69// absl::GetCurrentTimeNanos().
70// But this current implementation just uses GetCurrentTimeNanos().
71// TODO(user): implement it.
72class CycleTimer : public WallTimer {
73 public:
74 // This actually returns a number of nanoseconds instead of the number
75 // of CPU cycles.
76 int64_t GetCycles() const { return GetNanos(); }
77};
78
80
81// Conversion routines between CycleTimer::GetCycles and actual times.
83 public:
84 static int64_t SecondsToCycles(double s) {
85 return static_cast<int64_t>(s * 1e9);
86 }
87 static double CyclesToSeconds(int64_t c) { return c * 1e-9; }
88 static int64_t CyclesToMs(int64_t c) { return c / 1000000; }
89 static int64_t CyclesToUsec(int64_t c) { return c / 1000; }
90};
92
93// A WallTimer clone meant to support SetClock(), for unit testing. But for now
94// we just use WallTimer directly.
96
98 public:
99 // We do not own the pointer. The pointer must be valid for the duration
100 // of the existence of the ScopedWallTime instance. Not thread safe for
101 // aggregate_time.
102 explicit ScopedWallTime(double* aggregate_time);
104
105 private:
106 double* aggregate_time_;
107
108 // When the instance was created.
109 WallTimer timer_;
110
111 DISALLOW_COPY_AND_ASSIGN(ScopedWallTime);
112};
113#endif // OR_TOOLS_BASE_TIMER_H_
static int64_t CyclesToUsec(int64_t c)
Definition: timer.h:89
static double CyclesToSeconds(int64_t c)
Definition: timer.h:87
static int64_t CyclesToMs(int64_t c)
Definition: timer.h:88
static int64_t SecondsToCycles(double s)
Definition: timer.h:84
int64_t GetCycles() const
Definition: timer.h:76
~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
double Get() const
Definition: timer.h:45
int64_t GetNanos() const
Definition: timer.h:53
WallTimer UserTimer
Definition: timer.h:65
CycleTimerBase CycleTimerInstance
Definition: timer.h:91
WallTimer ClockTimer
Definition: timer.h:95
CycleTimer SimpleCycleTimer
Definition: timer.h:79