-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.h
61 lines (43 loc) · 1.03 KB
/
timer.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
#pragma once
#include <ctime>
#define DCLOCKS_SEC (static_cast<double>(CLOCKS_PER_SEC))
#define DCLOCKS_MSSEC (static_cast<double>(CLOCKS_PER_SEC) / 1000)
#ifdef _WIN32
class Timer
{
clock_t time;
public:
Timer() { set(); }
void set() { time = clock(); }
double get() { return (clock() - time) / DCLOCKS_SEC; }
double getms() { return (clock() - time) / DCLOCKS_MSSEC; }
};
#elif defined(__linux__) || defined(__unix__)
#include <sys/time.h>
#include <unistd.h>
class Timer
{
struct timeval time;
public:
Timer() { set(); }
void set() { gettimeofday(&time, NULL); }
double get()
{
struct timeval now;
long seconds, useconds;
gettimeofday(&now, NULL);
seconds = now.tv_sec - time.tv_sec;
useconds = now.tv_usec - time.tv_usec;
return seconds + useconds / 1000000000.0;
}
double getms()
{
struct timeval now;
long seconds, useconds;
gettimeofday(&now, NULL);
seconds = now.tv_sec - time.tv_sec;
useconds = now.tv_usec - time.tv_usec;
return seconds * 1000.0 + useconds / 1000000.0;
}
};
#endif