EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_timer.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ timer
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors:
27 #
28 ###############################################################################
29 */
30 
31 
37 #include "egs_timer.h"
38 #include "egs_functions.h"
39 
40 #ifdef WIN32
41 #include <ctime>
42 
43 class EGS_PrivateTimer {
44 public:
45  EGS_PrivateTimer() : mark(clock()) {};
46  unsigned long mark;
47  void start() {
48  mark = clock();
49  };
50  EGS_Float time() {
51  EGS_Float cpu = clock();
52  return (cpu - mark)/CLOCKS_PER_SEC;
53  };
54 };
55 
56 #else
57 
58 #include <sys/times.h>
59 #include <unistd.h>
60 
61 clock_t clps = 0;
62 
63 #ifndef SKIP_DOXYGEN
64 
70 class EGS_PrivateTimer {
71 public:
72  tms tstart, tend;
73  EGS_PrivateTimer() {
74  if (!clps) {
75  clps = sysconf(_SC_CLK_TCK);
76  }
77  times(&tstart);
78  };
79  void start() {
80  if (times(&tstart) < 0) {
81  egsWarning(" times returned < 0???\n");
82  }
83  };
84  EGS_Float time() {
85  times(&tend);
86  EGS_Float cpu = tend.tms_utime;
87  return (cpu - tstart.tms_utime)/clps;
88  };
89 };
90 #endif
91 
92 #endif
93 
95  p = new EGS_PrivateTimer;
96 }
97 
99  delete p;
100 }
101 
103  p->start();
104 }
105 
106 EGS_Float EGS_Timer::time() {
107  return p->time();
108 }
109 
EGS_Float time()
Returns the CPU time in seconds since start() was called.
Definition: egs_timer.cpp:106
EGS_Timer()
Construct an EGS_Timer object.
Definition: egs_timer.cpp:94
~EGS_Timer()
Destructor.
Definition: egs_timer.cpp:98
Global egspp functions header file.
void start()
Starts the time measurement.
Definition: egs_timer.cpp:102
EGS_Timer class header file.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.