Lagrange
timing.h
1/*
2 * Copyright 2017 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/Logger.h>
15
16#include <chrono>
17#include <string>
18
19namespace lagrange {
20
21using timestamp_type = std::chrono::time_point<std::chrono::steady_clock>;
22
23inline void get_timestamp(timestamp_type* t)
24{
25 *t = std::chrono::steady_clock::now();
26}
27
28inline timestamp_type get_timestamp()
29{
30 return std::chrono::steady_clock::now();
31}
32
33inline double timestamp_diff_in_seconds(timestamp_type start, timestamp_type end)
34{
35 std::chrono::duration<double> p = end - start;
36 return p.count();
37}
38
39inline double timestamp_diff_in_seconds(timestamp_type start)
40{
41 std::chrono::duration<double> p = get_timestamp() - start;
42 return p.count();
43}
44
49{
50public:
59 std::string prefix = "",
60 spdlog::logger* log = nullptr,
61 spdlog::level::level_enum level = spdlog::level::debug)
62 : m_prefix(std::move(prefix))
63 , m_logger(log ? log : &logger())
64 , m_level(level)
65 {}
66
70 void tick() { m_start_time = get_timestamp(); }
71
79 double tock(const std::string name = "")
80 {
81 auto end_time = get_timestamp();
82 auto duration = timestamp_diff_in_seconds(m_start_time, end_time);
83 m_logger->log(m_level, "{}{} time: {} (s)", m_prefix, name, duration);
84 return duration;
85 }
86
87private:
88 std::string m_prefix;
89 spdlog::logger* m_logger = nullptr;
90 spdlog::level::level_enum m_level = spdlog::level::debug;
91 timestamp_type m_start_time;
92};
93
98{
99public:
108 std::string prefix,
109 spdlog::logger* log = nullptr,
110 spdlog::level::level_enum level = spdlog::level::debug)
111 : m_timer(std::make_unique<VerboseTimer>(prefix, log, level))
112 {
113 m_timer->tick();
114 }
115
117 {
118 if (m_timer) {
119 m_timer->tock();
120 }
121 }
122
123 ScopedTimer(ScopedTimer&&) = default;
124 ScopedTimer& operator=(ScopedTimer&&) = default;
125
126private:
127 // Because RVO is not guaranteed until C++17, we need to store the content of the class in a
128 // moveable unique_ptr. Otherwise we can't write helper functions that return a ScopedTimer.
129 // Once we move to C++17, we should update this class and delete its move/copy operators.
130 std::unique_ptr<VerboseTimer> m_timer;
131};
132
137{
138public:
142 void tick() { m_start_time = get_timestamp(); }
143
149 double tock(const std::string name = "")
150 {
151 (void)name;
152 auto end_time = get_timestamp();
153 return timestamp_diff_in_seconds(m_start_time, end_time);
154 }
155
156private:
157 timestamp_type m_start_time;
158};
159
164{
165public:
169 void reset() { m_start = m_last = get_timestamp(); }
170
176 double interval()
177 {
178 auto last = m_last;
179 m_last = get_timestamp();
180 return timestamp_diff_in_seconds(last, m_last);
181 }
182
188 double total() { return timestamp_diff_in_seconds(m_start); }
189
190private:
191 timestamp_type m_start;
192 timestamp_type m_last;
193};
194
195
196} // namespace lagrange
Similar to a VerboseTimer, but uses RAII to call tick()/tock().
Definition: timing.h:98
ScopedTimer(std::string prefix, spdlog::logger *log=nullptr, spdlog::level::level_enum level=spdlog::level::debug)
Constructs a new instance.
Definition: timing.h:107
A timer that keeps track of a total time as well as intervals.
Definition: timing.h:164
double interval()
Returns current interval time (in seconds) and resets interval to now.
Definition: timing.h:176
double total()
Returns total time.
Definition: timing.h:188
void reset()
Starts/restarts the timer.
Definition: timing.h:169
A timer that does not print after tock()
Definition: timing.h:137
void tick()
Starts the timer.
Definition: timing.h:142
double tock(const std::string name="")
Stops the timer.
Definition: timing.h:149
Creates a verbose timer that prints after tock().
Definition: timing.h:49
VerboseTimer(std::string prefix="", spdlog::logger *log=nullptr, spdlog::level::level_enum level=spdlog::level::debug)
Constructs a new instance.
Definition: timing.h:58
void tick()
Starts the timer.
Definition: timing.h:70
double tock(const std::string name="")
Stops the timer.
Definition: timing.h:79
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
Main namespace for Lagrange.
Definition: AABBIGL.h:30