-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.c
52 lines (38 loc) · 1.08 KB
/
timer.c
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
#include "timer.h"
#ifdef _WIN32 // Windows system specific
#include <windows.h>
#else // Unix based system specific
#include <sys/time.h>
#endif
#ifdef _WIN32
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER startCount;
LARGE_INTEGER endCount;
#else
struct timeval startCount;
struct timeval endCount;
#endif
int started = 0;
void timer_start() {
started = 1; // reset stop flag
#ifdef _WIN32
QueryPerformanceCounter(&startCount);
QueryPerformanceFrequency(&frequency);
#else
gettimeofday(&startCount, NULL);
#endif
}
double timer_stop() {
if (!started)
return 0.0;
#ifdef _WIN32
QueryPerformanceCounter(&endCount);
double startTimeInSec = (double)startCount.QuadPart / frequency.QuadPart;
double endTimeInSec = (double)endCount.QuadPart / frequency.QuadPart;
#else
gettimeofday(&endCount, NULL);
double startTimeInSec = startCount.tv_sec + startCount.tv_usec / 1000000.0;
double endTimeInSec = endCount.tv_sec + endCount.tv_usec / 1000000.0;
#endif
return endTimeInSec - startTimeInSec;
}