Nori  24
timer.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob
5 
6  Nori is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Nori is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #if !defined(__NORI_TIMER_H)
20 #define __NORI_TIMER_H
21 
22 #include <nori/common.h>
23 #include <chrono>
24 
25 NORI_NAMESPACE_BEGIN
26 
32 class Timer {
33 public:
35  Timer() { reset(); }
36 
38  void reset() { start = std::chrono::system_clock::now(); }
39 
41  double elapsed() const {
42  auto now = std::chrono::system_clock::now();
43  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
44  return (double) duration.count();
45  }
46 
48  std::string elapsedString(bool precise = false) const {
49  return timeString(elapsed(), precise);
50  }
51 
53  double lap() {
54  auto now = std::chrono::system_clock::now();
55  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
56  start = now;
57  return (double) duration.count();
58  }
59 
61  std::string lapString(bool precise = false) {
62  return timeString(lap(), precise);
63  }
64 private:
65  std::chrono::system_clock::time_point start;
66 };
67 
68 NORI_NAMESPACE_END
69 
70 #endif /* __NORI_TIMER_H */
Simple timer with millisecond precision.
Definition: timer.h:32
std::string elapsedString(bool precise=false) const
Like elapsed(), but return a human-readable string.
Definition: timer.h:48
Timer()
Create a new timer and reset it.
Definition: timer.h:35
double elapsed() const
Return the number of milliseconds elapsed since the timer was last reset.
Definition: timer.h:41
std::string lapString(bool precise=false)
Like lap(), but return a human-readable string.
Definition: timer.h:61
void reset()
Reset the timer to the current time.
Definition: timer.h:38
double lap()
Return the number of milliseconds elapsed since the timer was last reset and then reset it.
Definition: timer.h:53