visageSDK
Stopwatch.h
1 //
2 // Created by kbesenic on 29.6.2017..
3 //
4 
5 #ifndef SPICAR_STOPWATCH_H
6 #define SPICAR_STOPWATCH_H
7 
8 #include <time.h>
9 #include <sys/time.h>
10 
11 #ifdef __MACH__
12 #include <mach/clock.h>
13 #include <mach/mach.h>
14 #endif
15 
16 class Stopwatch
17 {
18 
19 double getticks()
20 {
21  struct timespec ts;
22 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
23  clock_serv_t cclock;
24  mach_timespec_t mts;
25  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
26  clock_get_time(cclock, &mts);
27  mach_port_deallocate(mach_task_self(), cclock);
28  ts.tv_sec = mts.tv_sec;
29  ts.tv_nsec = mts.tv_nsec;
30 
31 #else
32  if(clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
33  return -1.0;
34 #endif
35  return (double) (ts.tv_sec*1000.0 + 1e-6*ts.tv_nsec);
36  //return -1.0;
37 }
38 
39 private:
40  double startTics;
41  double elapsedms;
42 
43 public:
44  void start() {startTics = getticks();}
45  double getTime() {elapsedms = (getticks() - startTics); return elapsedms;}
46 };
47 
48 #endif //SPICAR_STOPWATCH_H
49