12 void Clock::start_clock() {
18 getrusage(RUSAGE_SELF, &r);
19 m_rstart = r.ru_utime.tv_sec + r.ru_utime.tv_usec * 1.0e-6;
21 gettimeofday(&t, (
struct timezone *)0);
22 m_tstart = t.tv_sec + t.tv_usec * 1.0e-6;
25 m_mach_start = mach_absolute_time();
29 READ_CYCLE_COUNTER(cycle_start);
33 void Clock::stop_clock() {
39 getrusage(RUSAGE_SELF, &r);
40 m_rend = r.ru_utime.tv_sec + r.ru_utime.tv_usec * 1.0e-6;
42 gettimeofday(&t, (
struct timezone *)0);
43 m_tend = t.tv_sec + t.tv_usec * 1.0e-6;
47 m_mach_end = mach_absolute_time();
51 READ_CYCLE_COUNTER(m_cycle_end);
55 void Clock::print_elapsed(std::ostream &stream) {
56 std::streamsize w = stream.width(1);
57 std::streamsize p = stream.precision(2);
58 stream <<
" " << (float)((
double)(m_end - m_start) / CLOCKS_PER_SEC)
59 <<
" seconds elapsed (clock())\n";
60 stream <<
" " << (float)(m_rend - m_rstart) <<
" seconds elapsed (CPU)\n";
61 stream <<
" " << (float)(m_tend - m_tstart) <<
" seconds elapsed (wallclock)\n";
66 uint64_t mach_elapsed = mach_time_to_sec(m_mach_end - m_mach_start);
67 stream <<
"Mach processor cycle time = " << (float)m_mach_elapsed <<
"\n";
71 uint64_t cycle_elapsed = m_cycle_end - m_cycle_start;
72 stream << cycle_elapsed <<
" cycles elapsed\n";
73 stream << (float) ((
double)cycle_elapsed/(
double)CYCLES_PER_SEC << " seconds elapsed (cycle timer)\n";
78 double Clock::elapsed_time() {
return mach_time_to_sec(mach_absolute_time() - mach_start); }
80 double Clock::mach_time_to_sec(uint64_t elapsed) {
81 if (sTimebaseInfo.denom == 0) {
83 (void)mach_timebase_info(&sTimebaseInfo);
86 double ms = (double)(elapsed) / 1.0e9;
87 ms *= sTimebaseInfo.numer / sTimebaseInfo.denom;