MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 *
9 * - WallTimer: A real world "user" time timer.
10 * Useful for timings displayed to the user.
11 * - ClockTimer: A timer that measures execution times.
12 * Useful for measuring performance of the application because it excludes
13 * I/O preemtion and runs faster when multiple threads are running.
14 */
15#ifndef UTIL_TIMER_HEADER
16#define UTIL_TIMER_HEADER
17
18#include <chrono>
19#include <ctime>
20
21#include "util/defines.h"
22
24
30{
31private:
32 typedef std::chrono::high_resolution_clock TimerClock;
33 typedef std::chrono::time_point<TimerClock> TimerTimePoint;
34 typedef std::chrono::duration<double, std::milli> TimerDurationMs;
35
36 TimerTimePoint start;
37
38public:
39 WallTimer (void);
40 void reset (void);
41
43 std::size_t get_elapsed (void) const;
44
46 float get_elapsed_sec (void) const;
47};
48
49/* ---------------------------------------------------------------- */
50
64{
65private:
66 std::size_t start;
67
68public:
69 ClockTimer (void);
70 void reset (void);
71
72 static float now_sec (void);
73 static std::size_t now (void);
74
75 std::size_t get_elapsed (void) const;
76 float get_elapsed_sec (void) const;
77};
78
79/* ---------------------------------------------------------------- */
80
81inline
82WallTimer::WallTimer (void)
83{
84 this->reset();
85}
86
87inline void
88WallTimer::reset (void)
89{
90 this->start = TimerClock::now();
91}
92
93inline std::size_t
94WallTimer::get_elapsed (void) const
95{
96 TimerDurationMs diff = TimerClock::now() - this->start;
97 return diff.count();
98}
99
100inline float
101WallTimer::get_elapsed_sec (void) const
102{
103 return static_cast<float>(this->get_elapsed()) / 1000.0f;
104}
105
106/* ---------------------------------------------------------------- */
107
108inline
109ClockTimer::ClockTimer (void)
110{
111 this->reset();
112}
113
114inline void
115ClockTimer::reset (void)
116{
117 this->start = ClockTimer::now();
118}
119
120inline float
121ClockTimer::now_sec (void)
122{
123 return (float)std::clock() / (float)CLOCKS_PER_SEC;
124}
125
126inline std::size_t
127ClockTimer::now (void)
128{
129 return ((std::size_t)(std::clock()) * 1000) / (std::size_t)CLOCKS_PER_SEC;
130}
131
132inline float
133ClockTimer::get_elapsed_sec (void) const
134{
135 return (1.0f / 1000.0f) * (float)this->get_elapsed();
136}
137
138inline std::size_t
139ClockTimer::get_elapsed (void) const
140{
141 return ClockTimer::now() - start;
142}
143
145
146#endif /* UTIL_TIMER_HEADER */
Simple timer class to take execution times.
Definition timer.h:64
Cross-platform high-resolution real-time timer.
Definition timer.h:30
#define UTIL_NAMESPACE_BEGIN
Definition defines.h:13
#define UTIL_NAMESPACE_END
Definition defines.h:14