/** * @brief Time unit enum. * */ enum classtimeunit { /** second */ s, /** millisecond */ ms, /** microsecond */ us, };
/** * @brief Get current clock time for monotone increment time. * * @param unit time unit * @return uint64_t clock time */ uint64_tclocktime(timeunit unit);
uint64_tclocktime(timeunit unit) { // for C++11 and later auto run_time = std::chrono::steady_clock::now(); auto run_time_duration = std::chrono::duration_cast<std::chrono::microseconds>(run_time.time_since_epoch()).count(); auto us = static_cast<uint64_t>(run_time_duration);
uint64_t t = 0; switch (unit) { case timeunit::s: t = us2s(us); break; case timeunit::ms: t = us2ms(us); break; case timeunit::us: t = us; break; default: break; } return t; }
auto s = cutl::clocktime(cutl::timeunit::s); auto ms = cutl::clocktime(cutl::timeunit::ms); auto us = cutl::clocktime(cutl::timeunit::us); std::cout << "duration time from system start( s): " << s << std::endl; std::cout << "duration time from system start(ms): " << ms << std::endl; std::cout << "duration time from system start(us): " << us << std::endl; }
3.4. 运行结果
1 2 3 4
-------------------------------------------TestClocktime-------------------------------------------- duration time from system start( s): 1763153 duration time from system start(ms): 1763153809 duration time from system start(us): 1763153809208