-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_elapsed.c
80 lines (65 loc) · 1.75 KB
/
clock_elapsed.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#ifdef __MACH__
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif
void *
start_timer()
{
#ifndef __MACH__
struct timespec *tp = (struct timespec *)malloc(sizeof(struct timespec));
clock_gettime(CLOCK_REALTIME, tp);
return (void *)tp;
#else /* def __MACH__ */
uint64_t *start = (uint64_t *)malloc(sizeof(uint64_t));
*start = mach_absolute_time();
return (void *)start;
#endif /* def __MACH__ */
}
double
elapsed_timer(void *timer_data)
{
if (timer_data == NULL) {
return 0.0;
}
double elapsed;
#ifndef __MACH__
struct timespec start = *(struct timespec *)timer_data;
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
if (now.tv_nsec < start.tv_nsec) {
now.tv_nsec += 1 * 1000 * 1000 * 1000;
now.tv_sec--;
}
elapsed = (double)(now.tv_nsec - start.tv_nsec);
elapsed /= (double)(1 * 1000 * 1000 * 1000);
elapsed += (double)(now.tv_sec - start.tv_sec);
#else /* def __MACH__ */
static mach_timebase_info_data_t sTimebaseInfo;
uint64_t start = *(uint64_t *)timer_data;
uint64_t now = mach_absolute_time();
elapsed = (double)(now - start);
elapsed /= (double)(1 * 1000 * 1000 * 1000);
if ( sTimebaseInfo.denom == 0 ) {
(void) mach_timebase_info(&sTimebaseInfo);
}
elapsed *= ((double)sTimebaseInfo.numer) / ((double)sTimebaseInfo.denom);
#endif /* def __MACH__ */
return elapsed;
}
double
elapsed_timer_free(void ** timer_data_p)
{
double elapsed = 0;
if (timer_data_p == NULL) {
return elapsed;
}
elapsed = elapsed_timer(*timer_data_p);
free(*timer_data_p);
*timer_data_p = NULL;
return elapsed;
}