13 #include <mach/clock.h>
14 #include <mach/mach.h>
30 struct timespec tsstart;
33 struct timespec tsstop;
40 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
43 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
44 clock_get_time(cclock, &mts);
45 mach_port_deallocate(mach_task_self(), cclock);
46 tsstop.tv_sec = mts.tv_sec;
47 tsstop.tv_nsec = mts.tv_nsec;
49 clock_gettime(CLOCK_REALTIME, &tsstop);
58 :running(false),cstart(0),cstop(0)
72 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
75 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
76 clock_get_time(cclock, &mts);
77 mach_port_deallocate(mach_task_self(), cclock);
78 tsstart.tv_sec = mts.tv_sec;
79 tsstart.tv_nsec = mts.tv_nsec;
81 clock_gettime(CLOCK_REALTIME, &tsstart);
93 if (running ==
false)
return;
107 return ((
double)(tsstop.tv_sec - tsstart.tv_sec)) + ((1e-9) * ((
double)(tsstop.tv_nsec - tsstart.tv_nsec)));
119 return (((
double)(cstop - cstart)) / CLOCKS_PER_SEC);
timer()
Default constructor.
double getwct()
Return the elapsed real time.
double getcputime()
Return the cpu time.
void start()
Start the timer.
void reset()
Reset the timer.
Class for cpu time benchmarking.
void stop()
Stop the timer.