1 #ifndef SCX_RUNTIMER_HPP
2 #define SCX_RUNTIMER_HPP
7 #include <mach/clock.h>
11 #define SCX_BENCH(tag, times, loops, i, j, \
13 beforeStart, afterStart, \
14 beforeStop, afterStop) \
16 scx::RunTimer scx_timer; \
18 for (int i = 0; i < times; ++i) { \
22 for (int j = 0; j < loops; ++j) { \
27 scx_sum += scx_timer.DiffMS(); \
30 std::cout << tag << (scx_sum / times) << std::endl; \
33 #define SCX_BENCH0(tag, times, count, onLoop) \
34 SCX_BENCH(tag, times, count, i, j, \
39 #define SCX_BENCH1(tag, times, count, onLoop, \
41 SCX_BENCH(tag, times, count, i, j, \
46 #define SCX_BENCH4(tag, times, count, onLoop, \
48 SCX_BENCH(tag, times, count, i, j, \
53 #define SCX_BENCH14(tag, times, count, onLoop, \
54 beforeStart, afterStop) \
55 SCX_BENCH(tag, times, count, i, j, \
70 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK
, &cclock
);
71 clock_get_time(cclock
, &mts
);
72 mach_port_deallocate(mach_task_self(), cclock
);
73 mBegin
.tv_sec
= mts
.tv_sec
;
74 mBegin
.tv_nsec
= mts
.tv_nsec
;
76 clock_gettime(CLOCK_MONOTONIC_PRECISE
, &mBegin
);
85 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK
, &cclock
);
86 clock_get_time(cclock
, &mts
);
87 mach_port_deallocate(mach_task_self(), cclock
);
88 mEnd
.tv_sec
= mts
.tv_sec
;
89 mEnd
.tv_nsec
= mts
.tv_nsec
;
91 clock_gettime(CLOCK_MONOTONIC_PRECISE
, &mEnd
);
97 std::cout
<< (time_t)DiffMS() << std::endl
;
102 std::cout
<< (time_t)DiffUS() << std::endl
;
105 double DiffMS() const
107 return DiffUS() / 1000.f
;
110 double DiffUS() const
112 return (mEnd
.tv_sec
- mBegin
.tv_sec
)*1000000.f
+ (mEnd
.tv_nsec
- mBegin
.tv_nsec
)/1000.f
;