ExecuteTimer::ExecuteTimer() { memset(m_ctime, 0, sizeof(char)*32); //Reset the begin time. Reset(); } ExecuteTimer::~ExecuteTimer() { } char* ExecuteTimer::GetCTime() { double lt = GetDTime(); sprintf(m_ctime, "%.3fs", lt); return m_ctime; } double ExecuteTimer::GetDTime() { struct timespec end; clock_gettime(CLOCK_REALTIME, &end); //trans. to second unit. double lt = (end.tv_sec - m_begin.tv_sec) + (end.tv_nsec - m_begin.tv_nsec) * 1.0e-9; return lt; } void ExecuteTimer::Reset() { clock_gettime(CLOCK_REALTIME, &m_begin); } std::ostream& operator << (std::ostream& os, ExecuteTimer& obj) { double lt = obj.GetDTime(); //output formated string. char fmt[32] = {0}; sprintf(fmt, "%.3fs", lt); os << fmt; return os; } std::ofstream& operator << (std::ofstream& ofs, ExecuteTimer& obj) { double lt = obj.GetDTime(); //output formated string. char fmt[32] = {0}; sprintf(fmt, "%.3fs", lt); ofs << fmt; return ofs; }