-
Notifications
You must be signed in to change notification settings - Fork 3
/
StopWatch.h
61 lines (48 loc) · 1.22 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
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
class StopWatch
{
private:
LONGLONG _freq;
LARGE_INTEGER _begin;
LARGE_INTEGER _end;
public:
double costTime;
StopWatch(void)
{
LARGE_INTEGER tmp;
QueryPerformanceFrequency(&tmp);
_freq = tmp.QuadPart;
costTime = 0;
}
~StopWatch(void)
{
}
void Start() // 开始计时
{
QueryPerformanceCounter(&_begin);
}
void End() // 结束计时
{
QueryPerformanceCounter(&_end);
costTime = ((_end.QuadPart - _begin.QuadPart)*1000000.0f / _freq);
//printf("_begin:%I64d, _end:%I64d, costTime:%f\n", _begin.QuadPart, _end.QuadPart, costTime);
}
void Reset() // 计时清0
{
costTime = 0;
}
static void SleepPerformUS(DWORD usec)
{
LARGE_INTEGER perfCnt, start, now;
QueryPerformanceFrequency(&perfCnt);
QueryPerformanceCounter(&start);
do {
QueryPerformanceCounter((LARGE_INTEGER*)&now);
} while ((now.QuadPart - start.QuadPart) / double(perfCnt.QuadPart) * 1000000 < usec);
}
};
#endif // STOPWATCH_H