-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathtimer.cxx
More file actions
122 lines (109 loc) · 2.71 KB
/
timer.cxx
File metadata and controls
122 lines (109 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file timer.cxx
/// \author David Rohr
#include "timer.h"
#ifdef _WIN32
#include <windows.h>
#include <winbase.h>
#elif defined(__MACH__) || defined(__APPLE__)
#include <mach/clock.h>
#include <mach/mach.h>
#else
#include <ctime>
#endif
inline double HighResTimer::GetTime()
{
#ifdef _WIN32
__int64 istart;
QueryPerformanceCounter((LARGE_INTEGER*)&istart);
return ((double)istart);
#elif defined(__MACH__) || defined(__APPLE__)
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
return ((double)mts.tv_sec * 1.0e9 + (double)mts.tv_nsec);
#else
timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return ((double)tv.tv_sec * 1.0e9 + (double)tv.tv_nsec);
#endif
}
inline double HighResTimer::GetFrequency()
{
#ifdef _WIN32
__int64 ifreq;
QueryPerformanceFrequency((LARGE_INTEGER*)&ifreq);
return ((double)ifreq);
#else
return (1.0e9);
#endif
}
void HighResTimer::Start()
{
StartTime = GetTime();
running = 1;
}
void HighResTimer::ResetStart()
{
ElapsedTime = 0;
Start();
}
void HighResTimer::Stop()
{
if (running == 0) {
return;
}
running = 0;
double EndTime = 0;
EndTime = GetTime();
ElapsedTime += EndTime - StartTime;
}
void HighResTimer::StopAndStart(HighResTimer& startTimer)
{
if (running == 0) {
return;
}
running = 0;
double EndTime = 0;
EndTime = GetTime();
ElapsedTime += EndTime - StartTime;
startTimer.StartTime = EndTime;
startTimer.running = 1;
}
void HighResTimer::Reset()
{
ElapsedTime = 0;
StartTime = 0;
running = 0;
}
double HighResTimer::GetElapsedTime() { return ElapsedTime / Frequency; }
double HighResTimer::GetCurrentElapsedTime(bool reset)
{
if (running == 0) {
double retVal = GetElapsedTime();
if (reset) {
Reset();
}
return (retVal);
}
double CurrentTime = GetTime();
double retVal = (CurrentTime - StartTime + ElapsedTime) / Frequency;
if (reset) {
ElapsedTime = 0;
Start();
}
return (retVal);
}
void HighResTimer::AddTime(double t) { ElapsedTime += t * Frequency; }
double HighResTimer::Frequency = HighResTimer::GetFrequency();