Google OR-Tools v9.14
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-2025 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/time/clock.h"
20#include "absl/time/time.h"
21
22class WallTimer {
23 public:
25 void Reset() {
26 running_ = false;
27 sum_ = 0;
28 }
29 // When Start() is called multiple times, only the most recent is used.
30 void Start() {
31 running_ = true;
32 start_ = absl::GetCurrentTimeNanos();
33 }
34 void Restart() {
35 sum_ = 0;
36 Start();
37 }
38 void Stop() {
39 if (running_) {
40 sum_ += absl::GetCurrentTimeNanos() - start_;
41 running_ = false;
42 }
43 }
44 double Get() const { return GetNanos() * 1e-9; }
45 int64_t GetInMs() const { return GetNanos() / 1000000; }
46 int64_t GetInUsec() const { return GetNanos() / 1000; }
47 inline absl::Duration GetDuration() const {
48 return absl::Nanoseconds(GetNanos());
49 }
50 bool IsRunning() const { return running_; }
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:
86
87 static int64_t SecondsToCycles(double s) {
88 return static_cast<int64_t>(s * 1e9);
89 }
90 static double CyclesToSeconds(int64_t c) { return c * 1e-9; }
91 static int64_t CyclesToMs(int64_t c) { return c / 1000000; }
92 static int64_t CyclesToUsec(int64_t c) { return c / 1000; }
93};
95
96// A WallTimer clone meant to support SetClock(), for unit testing. But for now
97// we just use WallTimer directly.
99
101 public:
102 // We do not own the pointer. The pointer must be valid for the duration
103 // of the existence of the ScopedWallTime instance. Not thread safe for
104 // aggregate_time.
105 explicit ScopedWallTime(double* aggregate_time);
107
108 private:
109 double* aggregate_time_;
110
111 // When the instance was created.
112 WallTimer timer_;
113};
114#endif // OR_TOOLS_BASE_TIMER_H_
Conversion routines between CycleTimer::GetCycles and actual times.
Definition timer.h:82
static int64_t SecondsToCycles(double s)
Definition timer.h:87
static int64_t CyclesToUsec(int64_t c)
Definition timer.h:92
CycleTimerBase & operator=(const CycleTimerBase &)=delete
CycleTimerBase(const CycleTimerBase &)=delete
static double CyclesToSeconds(int64_t c)
Definition timer.h:90
static int64_t CyclesToMs(int64_t c)
Definition timer.h:91
int64_t GetCycles() const
Definition timer.h:76
ScopedWallTime(double *aggregate_time)
Definition timer.cc:18
~ScopedWallTime()
Definition timer.cc:24
int64_t GetInUsec() const
Definition timer.h:46
bool IsRunning() const
Definition timer.h:50
double Get() const
Definition timer.h:44
WallTimer()
Definition timer.h:24
absl::Duration GetDuration() const
Definition timer.h:47
int64_t GetInMs() const
Definition timer.h:45
void Restart()
Definition timer.h:34
void Reset()
Definition timer.h:25
int64_t GetNanos() const
Definition timer.h:53
void Start()
When Start() is called multiple times, only the most recent is used.
Definition timer.h:30
void Stop()
Definition timer.h:38
WallTimer UserTimer
Definition timer.h:65
CycleTimerBase CycleTimerInstance
Definition timer.h:94
WallTimer ClockTimer
Definition timer.h:98
CycleTimer SimpleCycleTimer
Definition timer.h:79