Stopwatch.cc
Go to the documentation of this file.
1 #include <sys/times.h>
2 #include <unistd.h>
3 #include "Stopwatch.h"
4 
5 using namespace utl;
6 
7 
8 const long Stopwatch::fgClockTicks = sysconf(_SC_CLK_TCK);
9 
10 
12  fIsStopped(false),
13  fUserTimeSum(0),
14  fSystemTimeSum(0)
15 {
16  times(&fStart);
17 }
18 
19 
20 void
22 {
23  fIsStopped = true;
25 }
26 
27 
28 void
30 {
31  if (fIsStopped) {
32  times(&fStart);
33  fIsStopped = false;
34  }
35 }
36 
37 
38 void
40 {
41  if (!fIsStopped) {
42  struct tms stop;
43  times(&stop);
44  fUserTimeSum += stop.tms_utime - fStart.tms_utime;
45  fSystemTimeSum += stop.tms_stime - fStart.tms_stime;
46  fIsStopped = true;
47  }
48 }
49 
50 
51 double
53 {
54  if (fIsStopped)
55  switch (kind) {
56  case eTotal:
58  case eUser:
59  return double(fUserTimeSum)/fgClockTicks*second;
60  case eSystem:
61  return double(fSystemTimeSum)/fgClockTicks*second;
62  }
63  else {
64  struct tms stop;
65  times(&stop);
66  switch (kind) {
67  case eTotal:
68  return double(fUserTimeSum + fSystemTimeSum +
69  stop.tms_utime - fStart.tms_utime +
70  stop.tms_stime - fStart.tms_stime)/fgClockTicks*second;
71  case eUser:
72  return double(fUserTimeSum +
73  stop.tms_utime - fStart.tms_utime)/fgClockTicks*second;
74  case eSystem:
75  return double(fSystemTimeSum +
76  stop.tms_stime - fStart.tms_stime)/fgClockTicks*second;
77  }
78  }
79 
80  return 0;
81 }
82 
83 
84 // Configure (x)emacs for this file ...
85 // Local Variables:
86 // mode: c++
87 // End:
constexpr double second
Definition: AugerUnits.h:145
struct tms fStart
Definition: Stopwatch.h:35
clock_t fUserTimeSum
Definition: Stopwatch.h:36
void Reset()
Definition: Stopwatch.cc:21
void Start()
Definition: Stopwatch.cc:29
clock_t fSystemTimeSum
Definition: Stopwatch.h:37
bool fIsStopped
Definition: Stopwatch.h:34
static const long fgClockTicks
Definition: Stopwatch.h:33
double GetCPUTime(const CPUTime kind=eTotal)
Definition: Stopwatch.cc:52

, generated on Tue Sep 26 2023.