-
Notifications
You must be signed in to change notification settings - Fork 3
/
Timing.h
52 lines (42 loc) · 886 Bytes
/
Timing.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
#ifndef _Timing_h
#define _Timing_h
#include <string>
#include <stdio.h>
#ifdef _WIN32
#define WINDOWS_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define NOMINMAX
#include <windows.h> // QueryPerformanceFrequency, QueryPerformanceCounter
inline double GetTime()
{
unsigned long long counter, frequency;
QueryPerformanceCounter((LARGE_INTEGER*)(&counter));
QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
return (double)counter / (double)frequency;
}
#else
# include <sys/time.h>
# include <unistd.h>
inline double GetTime()
{
timeval tv;
gettimeofday( &tv, NULL );
return (double)(tv.tv_sec*1000000+tv.tv_usec)/1000000.0;
}
#endif
class ScopedTimer
{
std::string name;
double startTime;
public:
ScopedTimer(const char* name)
{
this->name = name;
startTime = GetTime();
}
~ScopedTimer()
{
printf("%s: %f\n", name.c_str(), GetTime() - startTime);
}
};
#endif