Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.hh
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2019 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19// ----------------------------------------------------------------------
20// Class Timer
21//
22// Class description:
23//
24// Class for timer objects, able to measure elasped user/system process time.
25//
26// Note: Uses <sys/times.h> & <unistd.h> - POSIX.1 defined
27// If used, this header must be included in the source (.cc) file and it
28// must be the first header file to be included!
29//
30// Member functions:
31//
32// Timer()
33// Construct a timer object
34// Start()
35// Start timing
36// Stop()
37// Stop timing
38// bool IsValid()
39// Return true if have a valid time (ie start() and stop() called)
40// double GetRealElapsed()
41// Return the elapsed real time between last calling start() and stop()
42// double GetSystemElapsed()
43// Return the elapsed system time between last calling start() and stop()
44// double GetUserElapsed()
45// Return the elapsed user time between last calling start() and stop()
46//
47// Operators:
48//
49// std::ostream& operator << (std::ostream& os, const Timer& t);
50// Print the elapsed real,system and usertimes on os. Prints **s for times
51// if !IsValid
52//
53// Member data:
54//
55// bool fValidTimes
56// True after start and stop have both been called more than once and
57// an equal number of times
58// clock_t fStartRealTime,fEndRealTime
59// Real times (arbitrary time 0)
60// tms fStartTimes,fEndTimes
61// Timing structures (see times(2)) for start and end times
62
63// History:
64// 23.08.96 P.Kent Updated to also computed real elapsed time
65// 21.08.95 P.Kent
66// 29.04.97 G.Cosmo Added timings for Windows/NT
67
68#pragma once
69
70#include "PTL/Types.hh"
71
72#include <chrono>
73#include <iomanip>
74#include <sstream>
75
76#if !(defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64))
77# include <sys/times.h>
78# include <time.h>
79#else
80# include <ctime>
81# define _SC_CLK_TCK 1
82
83extern "C"
84{
85 int sysconf(int);
86};
87
88// Structure returned by times()
89
90struct tms
91{
92 clock_t tms_utime; /* user time */
93 clock_t tms_stime; /* system time */
94 clock_t tms_cutime; /* user time, children */
95 clock_t tms_cstime; /* system time, children */
96};
97
98extern "C"
99{
100 extern clock_t times(struct tms*);
101};
102#endif /* WIN32 */
103
104namespace PTL
105{
106class Timer
107{
108public:
110
111public:
112 void Start();
113 void Stop();
114 bool IsValid() const;
115 double GetRealElapsed() const;
116 double GetSystemElapsed() const;
117 double GetUserElapsed() const;
118
119 static const char* GetClockTime();
120
121private:
122 bool fValidTimes{ false };
123 using clock_type = std::chrono::high_resolution_clock;
124 std::chrono::time_point<clock_type> fStartRealTime, fEndRealTime;
125 tms fStartTimes, fEndTimes;
126};
127
128// ------------------------------------------------------------
129// class inline implementation
130// ------------------------------------------------------------
131
132inline void
134{
135 fValidTimes = false;
136 times(&fStartTimes);
137 fStartRealTime = clock_type::now();
138}
139
140inline void
142{
143 times(&fEndTimes);
144 fEndRealTime = clock_type::now();
145 fValidTimes = true;
146}
147
148inline bool
150{
151 return fValidTimes;
152}
153
154inline const char*
156{
157 time_t rawtime;
158 struct tm* timeinfo;
159
160 time(&rawtime);
161 timeinfo = localtime(&rawtime);
162 return asctime(timeinfo);
163}
164
165inline std::ostream&
166operator<<(std::ostream& os, const Timer& t)
167{
168 // so fixed doesn't propagate
169 std::stringstream ss;
170 ss << std::fixed;
171 if(t.IsValid())
172 {
173 ss << "Real=" << t.GetRealElapsed() << "s User=" << t.GetUserElapsed()
174 << "s Sys=" << t.GetSystemElapsed() << "s";
175
176 // avoid possible FPE error
177 if(t.GetRealElapsed() > 1.0e-6)
178 {
179 double cpu_util =
180 (t.GetUserElapsed() + t.GetSystemElapsed()) / t.GetRealElapsed() * 100.0;
181 ss << std::setprecision(1);
182 ss << " [Cpu=" << std::setprecision(1) << cpu_util << "%]";
183 }
184 }
185 else
186 {
187 ss << "Real=****s User=****s Sys=****s";
188 }
189 os << ss.str();
190
191 return os;
192}
193
194} // namespace PTL
#define PTL_DEFAULT_OBJECT(NAME)
Definition Types.hh:86
bool IsValid() const
Definition Timer.hh:149
void Start()
Definition Timer.hh:133
void Stop()
Definition Timer.hh:141
double GetUserElapsed() const
Definition Timer.cc:113
double GetRealElapsed() const
Definition Timer.cc:85
double GetSystemElapsed() const
Definition Timer.cc:99
static const char * GetClockTime()
Definition Timer.hh:155
std::ostream & operator<<(std::ostream &os, const Timer &t)
Definition Timer.hh:166