mightymandel v16

GPU-based Mandelbrot set explorer

stopwatch.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
5 #include "stopwatch.h"
6 
7 #ifndef MIGHTYMANDEL_WIN32
8 #ifndef __MACH__
9 
10 // posix with rt
11 
12 #include <stdlib.h>
13 #include <time.h>
14 
22 double time_difference(const struct timespec *t1, const struct timespec *t0) {
23  return (t1->tv_sec - t0->tv_sec) + (t1->tv_nsec - t0->tv_nsec) / 1000000000.0;
24 }
25 
26 struct stopwatch {
27  struct timespec start;
28  struct timespec stop;
29  double elapsed;
30 };
31 
33  return calloc(1, sizeof(struct stopwatch));
34 }
35 
36 void stopwatch_start(struct stopwatch *t) {
37  clock_gettime(CLOCK_MONOTONIC, &t->start);
38 }
39 
40 void stopwatch_stop(struct stopwatch *t) {
41  clock_gettime(CLOCK_MONOTONIC, &t->stop);
42  t->elapsed += time_difference(&t->stop, &t->start);
43 }
44 
45 #else
46 
47 // http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx
48 
49 #include <stdlib.h>
50 #include <mach/clock.h>
51 #include <mach/mach.h>
52 
60 double time_difference(const mach_timespec_t *t1, const mach_timespec_t *t0) {
61  return (t1->tv_sec - t0->tv_sec) + (t1->tv_nsec - (double) t0->tv_nsec) / 1000000000.0;
62 }
63 
64 struct stopwatch {
65  mach_timespec_t start;
66  mach_timespec_t stop;
67  double elapsed;
68 };
69 
70 struct stopwatch *stopwatch_new() {
71  return calloc(1, sizeof(struct stopwatch));
72 }
73 
74 void stopwatch_start(struct stopwatch *t) {
75  clock_serv_t cclock;
76  host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
77  clock_get_time(cclock, &t->start);
78  mach_port_deallocate(mach_task_self(), cclock);
79 }
80 
81 void stopwatch_stop(struct stopwatch *t) {
82  clock_serv_t cclock;
83  host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
84  clock_get_time(cclock, &t->stop);
85  mach_port_deallocate(mach_task_self(), cclock);
86  t->elapsed += time_difference(&t->stop, &t->start);
87 }
88 
89 #endif
90 #else
91 
92 // http://www.decompile.com/cpp/faq/windows_stopwatch_api.htm
93 
94 #include <stdlib.h>
95 #include <windows.h>
96 
105 double time_difference(const LARGE_INTEGER *t1, const LARGE_INTEGER *t0, const LARGE_INTEGER *ticks) {
106  LARGE_INTEGER delta;
107  delta.QuadPart = t1->QuadPart - t0->QuadPart;
108  return delta.QuadPart / (double) ticks->QuadPart;
109 }
110 
111 struct stopwatch {
112  LARGE_INTEGER start;
113  LARGE_INTEGER stop;
114  double elapsed;
115  LARGE_INTEGER ticks;
116 };
117 
118 struct stopwatch *stopwatch_new() {
119  struct stopwatch *t = calloc(1, sizeof(struct stopwatch));
120  QueryPerformanceFrequency(&t->ticks);
121  return t;
122 }
123 
124 void stopwatch_start(struct stopwatch *t) {
125  QueryPerformanceCounter(&t->start);
126 }
127 
128 void stopwatch_stop(struct stopwatch *t) {
129  QueryPerformanceCounter(&t->stop);
130  t->elapsed += time_difference(&t->stop, &t->start, &t->ticks);
131 }
132 
133 #endif
134 
135 void stopwatch_delete(struct stopwatch *t) {
136  free(t);
137 }
138 
139 void stopwatch_reset(struct stopwatch *t) {
140  stopwatch_stop(t);
141  stopwatch_start(t);
142  t->elapsed = 0;
143 }
144 
145 double stopwatch_elapsed(struct stopwatch *t) {
146  stopwatch_stop(t);
147  stopwatch_start(t);
148  return t->elapsed;
149 }