BOSS 7.0.4
BESIII Offline Software System
Loading...
Searching...
No Matches
BesTimer.cxx
Go to the documentation of this file.
1
2
3// this is -*- c++ -*-
4// $Id: BesTimer.cxx,v 1.4 2011/02/18 04:23:58 maqm Exp $
5// $Name: BesTimerSvc-00-00-12 $
6#include <sys/time.h>
7#include <complex>
8
9#include "BesTimerSvc/BesTimer.h"
10
11#define SecsInDay 86400
12
13 BesTimer::BesTimer (const std::string name)
14 : m_name (name) {
15 m_numberOfMeasurements = 0;
16 m_mean = 0.0;
17 m_ms = 0.0;
18 m_elapsed = 0.0;
19 m_started=false;
20 m_paused=false;
21 m_propVal = 0;
22 m_meanVal = 0;
23 m_meanTimePerObject = 0.0;
24 m_NmeanTimePerObject = 0;
25 }
26
27 void BesTimer::start( void ) {
28 struct timeval tv;
29
30 m_started = true;
31 m_paused = false;
32 m_elapsed = 0.0;
33 m_propVal = 0;
34 // if already started, the start time will be over-written
35 gettimeofday(&tv, 0);
36 m_startTime = tv;
37 }
38
39 void BesTimer::stop ( void ) {
40 struct timeval tv;
41
42 if (!m_started) return;
43
44 gettimeofday(&tv, 0);
45 m_started = false;
46
47 if (!m_paused) {
48 float mtime;
49 int secs, usecs;
50
51 if (tv.tv_sec >= m_startTime.tv_sec) {
52 secs = tv.tv_sec - m_startTime.tv_sec;
53 } else {
54 secs = tv.tv_sec - m_startTime.tv_sec + SecsInDay;
55 }
56 usecs = tv.tv_usec - m_startTime.tv_usec;
57 mtime = static_cast<float>(secs)*1000. + static_cast<float>(usecs)/1000.;
58
59 // elapsed time
60 m_elapsed += mtime;
61
62 }
63
64 // statistics - mean, rms
65 double denom = static_cast <double> (++m_numberOfMeasurements);
66 double d = m_elapsed - m_mean;
67 m_mean += d / denom;
68 double dd = m_elapsed*m_elapsed - m_ms;
69 m_ms += dd / denom;
70
71 // mean property
72 m_meanVal += static_cast <double> (m_propVal - m_meanVal) / denom;
73 if (m_propVal !=0) {
74 double timePerObject = m_elapsed/static_cast <double>(m_propVal);
75 m_meanTimePerObject += static_cast <double> (timePerObject - m_meanTimePerObject) /
76 static_cast<double> (++m_NmeanTimePerObject);
77 }
78 }
79
80 void BesTimer::pause ( void ) {
81 struct timeval tv;
82
83 // only pause if started
84 // a pause while paused is ignored
85 if (m_paused || !m_started) return;
86 m_paused = true;
87 gettimeofday(&tv, 0);
88
89 float mtime;
90 int secs, usecs;
91
92 if (tv.tv_sec >= m_startTime.tv_sec) {
93 secs = tv.tv_sec - m_startTime.tv_sec;
94 } else {
95 secs = tv.tv_sec - m_startTime.tv_sec + SecsInDay;
96 }
97 usecs = tv.tv_usec - m_startTime.tv_usec;
98 mtime = static_cast<float>(secs)*1000. + static_cast<float>(usecs)/1000.;
99 m_elapsed += mtime; // time so far
100 }
101
102 void BesTimer::resume( void ) {
103 struct timeval tv;
104
105 if (!m_started) {
106 start(); // resume acts as start if not started
107 } else if (m_paused) {
108 m_paused = false;
109 gettimeofday(&tv, 0);
110 m_startTime = tv;
111 }
112 }
113
115 if (!m_started) {
116 m_elapsed = 0.0;
117 m_propVal = 0;
118 }
119 }
#define SecsInDay
Definition: BesTimer.cxx:11
double m_mean
void pause(void)
Definition: BesTimer.cxx:80
void reset()
Definition: BesTimer.cxx:114
BesTimer(const std::string name)
Definition: BesTimer.cxx:13
void resume(void)
Definition: BesTimer.cxx:102
void start(void)
Definition: BesTimer.cxx:27
void stop(void)
Definition: BesTimer.cxx:39