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.cc
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// Implementation
23// 29.04.97 G.Cosmo Added timings for Windows systems
24
25#include "PTL/Timer.hh"
26
27#include <stdexcept>
28
29using namespace PTL;
30
31#if !(defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64))
32# include <unistd.h>
33#else
34# include <sys/types.h>
35# include <windows.h>
36
37//======================================================================================//
38
39// extract milliseconds time unit
40int
41sysconf(int a)
42{
43 if(a == _SC_CLK_TCK)
44 return 1000;
45 else
46 return 0;
47}
48
49//======================================================================================//
50
51static clock_t
52filetime2msec(FILETIME* t)
53{
54 return (clock_t)((((float) t->dwHighDateTime) * 429496.7296) +
55 (((float) t->dwLowDateTime) * .0001));
56}
57
58//======================================================================================//
59
60clock_t
61times(struct tms* t)
62{
63 FILETIME ct = { 0, 0 }, et = { 0, 0 }, st = { 0, 0 }, ut = { 0, 0 }, rt = { 0, 0 };
64 SYSTEMTIME realtime;
65
66 GetSystemTime(&realtime);
67 SystemTimeToFileTime(&realtime, &rt); // get real time in 10^-9 sec
68 if(t != 0)
69 {
70 GetProcessTimes(GetCurrentProcess(), &ct, &et, &st,
71 &ut); // get process time in 10^-9 sec
72 t->tms_utime = t->tms_cutime = filetime2msec(&ut);
73 t->tms_stime = t->tms_cstime = filetime2msec(&st);
74 }
75 return filetime2msec(&rt);
76}
77
78//======================================================================================//
79
80#endif /* WIN32 */
81
82//======================================================================================//
83
84double
86{
87 if(!fValidTimes)
88 {
89 throw std::runtime_error("Timer::GetRealElapsed() - "
90 "Timer not stopped or times not recorded!");
91 }
92 std::chrono::duration<double> diff = fEndRealTime - fStartRealTime;
93 return diff.count();
94}
95
96//======================================================================================//
97
98double
100{
101 if(!fValidTimes)
102 {
103 throw std::runtime_error("Timer::GetSystemElapsed() - "
104 "Timer not stopped or times not recorded!");
105 }
106 double diff = fEndTimes.tms_stime - fStartTimes.tms_stime;
107 return diff / sysconf(_SC_CLK_TCK);
108}
109
110//======================================================================================//
111
112double
114{
115 if(!fValidTimes)
116 {
117 throw std::runtime_error("Timer::GetUserElapsed() - "
118 "Timer not stopped or times not recorded!");
119 }
120 double diff = fEndTimes.tms_utime - fStartTimes.tms_utime;
121 return diff / sysconf(_SC_CLK_TCK);
122}
123
124//======================================================================================//
double GetUserElapsed() const
Definition Timer.cc:113
double GetRealElapsed() const
Definition Timer.cc:85
double GetSystemElapsed() const
Definition Timer.cc:99