-
Notifications
You must be signed in to change notification settings - Fork 32
/
Stopwatch.h
111 lines (95 loc) · 2.03 KB
/
Stopwatch.h
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
/*
AMD copyrights (Copyright (c) 2011 Advanced Micro Devices, Inc. All rights reserved)
*/
#pragma once
#if defined(__WINDOWS__)
#define NOMINMAX
#include <windows.h>
#define TIME_TYPE LARGE_INTEGER
#define QUERY_FREQ(f) QueryPerformanceFrequency(&f)
#define RECORD(t) QueryPerformanceCounter(&t)
#define GET_TIME(t) (t).QuadPart*1000.0
#define GET_FREQ(f) (f).QuadPart
#else
#include <sys/time.h>
#define TIME_TYPE timeval
#define QUERY_FREQ(f) f.tv_sec = 1
#define RECORD(t) gettimeofday(&t, 0)
#define GET_TIME(t) ((t).tv_sec*1000.0+(t).tv_usec/1000.0)
#define GET_FREQ(f) 1.0
#endif
class Stopwatch
{
public:
__inline
Stopwatch();
__inline
void init();
__inline
void start();
__inline
void split();
__inline
float getCurrent();
__inline
void stop();
__inline
float getMs();
__inline
void getMs( float* times, int capacity );
private:
enum
{
CAPACITY = 12,
};
int m_idx;
TIME_TYPE m_frequency;
TIME_TYPE m_t[CAPACITY];
};
__inline
Stopwatch::Stopwatch()
{
// QueryPerformanceFrequency( &m_frequency );
QUERY_FREQ( m_frequency );
}
__inline
void Stopwatch::start()
{
m_idx = 0;
// QueryPerformanceCounter(&m_t[m_idx++]);
RECORD( m_t[m_idx++] );
}
__inline
void Stopwatch::split()
{
// QueryPerformanceCounter(&m_t[m_idx++]);
RECORD( m_t[m_idx++] );
}
__inline
float Stopwatch::getCurrent()
{
TIME_TYPE t;
RECORD( t );
return (float)( GET_TIME(t) - GET_TIME(m_t[0]) )/GET_FREQ(m_frequency);
}
__inline
void Stopwatch::stop()
{
split();
}
__inline
float Stopwatch::getMs()
{
// return (float)(1000*(m_t[1].QuadPart - m_t[0].QuadPart))/m_frequency.QuadPart;
return (float)( GET_TIME( m_t[1] ) - GET_TIME( m_t[0] ) )/GET_FREQ( m_frequency );
}
__inline
void Stopwatch::getMs(float* times, int capacity)
{
for(int i=0; i<capacity; i++) times[i] = 0.f;
for(int i=0; i<std::min(capacity, m_idx-1); i++)
{
// times[i] = (float)(1000*(m_t[i+1].QuadPart - m_t[i].QuadPart))/m_frequency.QuadPart;
times[i] = (float)( GET_TIME( m_t[i+1] ) - GET_TIME( m_t[i] ) )/GET_FREQ( m_frequency );
}
}