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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
Mod Organizer archive handling
Copyright (C) 2020 MO2 Team. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ARCHIVE_INSTRUMENT_H
#define ARCHIVE_INSTRUMENT_H
#include <chrono>
#include <format>
namespace ArchiveTimers
{
/**
* If INSTRUMENT_ARCHIVE is not defined, we define a Timer() class that is empty and
* does nothing, and thus should be optimized out by the compiler.
*/
#ifdef INSTRUMENT_ARCHIVE
/**
* Small class that can be used to instrument portion of code using a guard.
*
*/
class Timer
{
public:
using clock_t = std::chrono::system_clock;
struct TimerGuard
{
TimerGuard(TimerGuard const&) = delete;
TimerGuard(TimerGuard&&) = delete;
TimerGuard& operator=(TimerGuard const&) = delete;
TimerGuard& operator=(TimerGuard&&) = delete;
~TimerGuard()
{
m_Timer.ncalls++;
m_Timer.time += clock_t::now() - m_Start;
}
private:
TimerGuard(Timer& timer) : m_Timer{timer}, m_Start{clock_t::now()} {}
Timer& m_Timer;
clock_t::time_point m_Start;
friend class Timer;
};
/**
*
*/
Timer() = default;
/**
* @brief Instrument a portion of code.
*
* Instrumenting is done by calling `instrument()` and storing the result in a local
* variable. The scope of the local guard variable is the scope of the
* instrumentation.
*
* @return a guard to instrument the code.
*/
TimerGuard instrument() { return {*this}; }
std::wstring toString(std::wstring const& name) const
{
auto ms = [](auto&& t) {
return std::chrono::duration<double, std::milli>(t);
};
return std::format(
L"Instrument '{}': {} calls, total of {}ms, {:.3f}ms per call on average.",
name, ncalls, ms(time).count(), ms(time).count() / ncalls);
}
private:
std::size_t ncalls{0};
clock_t::duration time{0};
};
#else
struct Timer
{
// This is the only method needed:
Timer& instrument() { return *this; }
};
#endif
} // namespace ArchiveTimers
#endif
|