Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1// Copyright 2010-2024 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 <cstdint>
18
19#include "absl/log/check.h"
20#include "absl/time/clock.h"
21#include "absl/time/time.h"
22#include "ortools/base/macros.h"
23
24class WallTimer {
25 public:
27 void Reset() {
28 running_ = false;
29 sum_ = 0;
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 void Restart() {
37 sum_ = 0;
38 Start();
39 }
40 void Stop() {
41 if (running_) {
42 sum_ += absl::GetCurrentTimeNanos() - start_;
43 running_ = false;
44 }
45 }
46 double Get() const { return GetNanos() * 1e-9; }
47 int64_t GetInMs() const { return GetNanos() / 1000000; }
48 int64_t GetInUsec() const { return GetNanos() / 1000; }
49 inline absl::Duration GetDuration() const {
50 return absl::Nanoseconds(GetNanos());
51 }
52 bool IsRunning() const { return running_; }
53
54 protected:
55 int64_t GetNanos() const {
56 return running_ ? absl::GetCurrentTimeNanos() - start_ + sum_ : sum_;
57 }
58
59 private:
60 bool running_;
61 int64_t start_;
62 int64_t sum_;
63};
64
65// This is meant to measure the actual CPU usage time.
66// TODO(user): implement it properly.
68
69// This is meant to be a ultra-fast interface to the hardware cycle counter,
70// without periodic recalibration, to be even faster than
71// absl::GetCurrentTimeNanos().
72// But this current implementation just uses GetCurrentTimeNanos().
73// TODO(user): implement it.
74class CycleTimer : public WallTimer {
75 public:
76 // This actually returns a number of nanoseconds instead of the number
77 // of CPU cycles.
78 int64_t GetCycles() const { return GetNanos(); }
79};
80
82
83// Conversion routines between CycleTimer::GetCycles and actual times.
85 public:
86 static int64_t SecondsToCycles(double s) {
87 return static_cast<int64_t>(s * 1e9);
88 }
89 static double CyclesToSeconds(int64_t c) { return c * 1e-9; }
90 static int64_t CyclesToMs(int64_t c) { return c / 1000000; }
91 static int64_t CyclesToUsec(int64_t c) { return c / 1000; }
92};
94
95// A WallTimer clone meant to support SetClock(), for unit testing. But for now
96// we just use WallTimer directly.
98
100 public:
101 // We do not own the pointer. The pointer must be valid for the duration
102 // of the existence of the ScopedWallTime instance. Not thread safe for
103 // aggregate_time.
104 explicit ScopedWallTime(double* aggregate_time);
106
107 private:
108 double* aggregate_time_;
109
110 // When the instance was created.
111 WallTimer timer_;
112
114};
115#endif // OR_TOOLS_BASE_TIMER_H_
Conversion routines between CycleTimer::GetCycles and actual times.
Definition timer.h:84
static int64_t SecondsToCycles(double s)
Definition timer.h:86
static int64_t CyclesToUsec(int64_t c)
Definition timer.h:91
static double CyclesToSeconds(int64_t c)
Definition timer.h:89
static int64_t CyclesToMs(int64_t c)
Definition timer.h:90
int64_t GetCycles() const
Definition timer.h:78
ScopedWallTime(double *aggregate_time)
Definition timer.cc:16
~ScopedWallTime()
Definition timer.cc:22
int64_t GetInUsec() const
Definition timer.h:48
bool IsRunning() const
Definition timer.h:52
double Get() const
Definition timer.h:46
WallTimer()
Definition timer.h:26
absl::Duration GetDuration() const
Definition timer.h:49
int64_t GetInMs() const
Definition timer.h:47
void Restart()
Definition timer.h:36
void Reset()
Definition timer.h:27
int64_t GetNanos() const
Definition timer.h:55
void Start()
When Start() is called multiple times, only the most recent is used.
Definition timer.h:32
void Stop()
Definition timer.h:40
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:29
WallTimer UserTimer
Definition timer.h:67
CycleTimerBase CycleTimerInstance
Definition timer.h:93
WallTimer ClockTimer
Definition timer.h:97
CycleTimer SimpleCycleTimer
Definition timer.h:81