-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimer.hpp
More file actions
84 lines (73 loc) · 1.88 KB
/
timer.hpp
File metadata and controls
84 lines (73 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//===--------------------------------------------------------------------------------*- C++ -*-===//
// _
// | |
// __| | __ ___ ___ ___
// / _` |/ _` \ \ /\ / / '_ |
// | (_| | (_| |\ V V /| | | |
// \__,_|\__,_| \_/\_/ |_| |_| - Compiler Toolchain
//
//
// This file is distributed under the MIT License (MIT).
// See LICENSE.txt for details.
//
//===------------------------------------------------------------------------------------------===//
#pragma once
#include <sstream>
#include <string>
#include <utility>
namespace gridtools {
namespace dawn {
/**
* @class Timer
* Measures total elapsed time between all start and stop calls
*/
template <typename TimerImpl>
class timer {
protected:
timer(std::string name) : m_name(std::move(name)) {}
public:
/**
* Reset counters
*/
void reset() {
m_total_time = 0;
m_counter = 0;
}
/**
* Start the stop watch
*/
void start() { impl().start_impl(); }
/**
* Pause the stop watch
*/
void pause() {
m_total_time += impl().pause_impl();
m_counter++;
}
/**
* @return total elapsed time [s]
*/
double total_time() const { return m_total_time; }
/**
* @return how often the timer was paused
*/
size_t count() const { return m_counter; }
/**
* @return total elapsed time [s] as string
*/
std::string to_string() const {
std::ostringstream out;
if(m_total_time < 0)
out << "\t[s]\t" << m_name << "NO_TIMES_AVAILABLE";
else
out << m_name << "\t[s]\t" << m_total_time << " (" << m_counter << "x called)";
return out.str();
}
private:
TimerImpl& impl() { return *static_cast<TimerImpl*>(this); }
std::string m_name;
double m_total_time = 0;
size_t m_counter = 0;
};
} // namespace dawn
} // namespace gridtools