OR-Tools  8.2
timer.h
Go to the documentation of this file.
1// Copyright 2010-2018 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 GetInMs() const { return GetNanos() / 1000000; }
47 int64 GetInUsec() const { return GetNanos() / 1000; }
48 inline absl::Duration GetDuration() const {
49 return absl::Nanoseconds(GetNanos());
50 }
51
52 protected:
53 int64 GetNanos() const {
54 return running_ ? absl::GetCurrentTimeNanos() - start_ + sum_ : sum_;
55 }
56
57 private:
58 bool running_;
59 int64 start_;
60 int64 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 GetCycles() const { return GetNanos(); }
77};
78
80
81// Conversion routines between CycleTimer::GetCycles and actual times.
83 public:
84 static int64 SecondsToCycles(double s) { return static_cast<int64>(s * 1e9); }
85 static double CyclesToSeconds(int64 c) { return c * 1e-9; }
86 static int64 CyclesToMs(int64 c) { return c / 1000000; }
87 static int64 CyclesToUsec(int64 c) { return c / 1000; }
88};
90
91// A WallTimer clone meant to support SetClock(), for unit testing. But for now
92// we just use WallTimer directly.
94
96 public:
97 // We do not own the pointer. The pointer must be valid for the duration
98 // of the existence of the ScopedWallTime instance. Not thread safe for
99 // aggregate_time.
100 explicit ScopedWallTime(double* aggregate_time);
102
103 private:
104 double* aggregate_time_;
105
106 // When the instance was created.
107 WallTimer timer_;
108
109 DISALLOW_COPY_AND_ASSIGN(ScopedWallTime);
110};
111#endif // OR_TOOLS_BASE_TIMER_H_
static double CyclesToSeconds(int64 c)
Definition: timer.h:85
static int64 CyclesToUsec(int64 c)
Definition: timer.h:87
static int64 SecondsToCycles(double s)
Definition: timer.h:84
static int64 CyclesToMs(int64 c)
Definition: timer.h:86
int64 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
void Reset()
Definition: timer.h:26
WallTimer()
Definition: timer.h:25
absl::Duration GetDuration() const
Definition: timer.h:48
int64 GetInMs() const
Definition: timer.h:46
int64 GetInUsec() const
Definition: timer.h:47
void Restart()
Definition: timer.h:35
int64 GetNanos() const
Definition: timer.h:53
double Get() const
Definition: timer.h:45
int64_t int64
WallTimer UserTimer
Definition: timer.h:65
CycleTimerBase CycleTimerInstance
Definition: timer.h:89
WallTimer ClockTimer
Definition: timer.h:93
CycleTimer SimpleCycleTimer
Definition: timer.h:79