-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictoc.h
70 lines (50 loc) · 1.25 KB
/
tictoc.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
#ifndef __UTILS_TIMER_TICTOC_H__
#define __UTILS_TIMER_TICTOC_H__
#ifdef __cplusplus
extern "C"{
#endif
#ifdef _WIN32
#include <windows.h>
typedef LARGE_INTEGER tic_t;
static tic_t tic()
{
LARGE_INTEGER curtic;
QueryPerformanceCounter(&curtic);
return curtic;
}
static double toc(tic_t * oldCount)
{
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER t1 = *oldCount, t2; // ticks
double elapsedTime;
// get ticks per second
QueryPerformanceFrequency(&frequency);
// stop timer
QueryPerformanceCounter(&t2);
// compute and print the elapsed time in millisec
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
return elapsedTime;
}
#else
#include <sys/time.h>
typedef struct timeval tic_t;
static tic_t tic()
{
timeval curtic;
gettimeofday(&curtic, NULL);
return curtic;
}
static double toc(tic_t * oldCount)
{
timeval newCount;
gettimeofday(&newCount, NULL);
double t = double(newCount.tv_sec - oldCount->tv_sec ) +
double(newCount.tv_usec - oldCount->tv_usec) * 1.e-9;
return (t * 1000.0);
}
#endif
typedef tic_t tictoc_t;
#ifdef __cplusplus
}
#endif
#endif