#ifndef _SPEEX_WEBRTC_TEST_TIMER_H_ #define _SPEEX_WEBRTC_TEST_TIMER_H_ #include #include #include #include namespace SpeexWebRTCTest { namespace _detail { template struct is_one_of; template struct is_one_of { static constexpr bool value = std::is_same::value; }; template struct is_one_of { static constexpr bool value = std::is_same::value || is_one_of::value; }; template ::value>::type> constexpr const char* duration_suffix() { if (std::is_same::value) { return "h"; } else if (std::is_same::value) { return "min"; } else if (std::is_same::value) { return "s"; } else if (std::is_same::value) { return "ms"; } else if (std::is_same::value) { return "us"; } else if (std::is_same::value) { return "ns"; } return ""; } template std::string format_impl(DurationIn) { return {}; } template std::string format_impl(DurationIn d) { std::string out; auto val = std::chrono::duration_cast(d); if (val.count() != 0) { out = std::to_string(val.count()) + duration_suffix(); if (sizeof...(RestDurations) > 0) { out += " " + format_impl(d - val); } } else { if (sizeof...(RestDurations) > 0) { out = format_impl(d); } else if (count == 0) { out = std::to_string(0) + duration_suffix(); } } return out; } } // namespace _detail template std::string format(DurationIn d) { return _detail::format_impl<0, DurationIn, RestDurations...>(d); } template class Timer final { public: explicit Timer(const QDebug& debug, QString name = "") : debug_(debug), name_(std::move(name)) { start_ = std::chrono::high_resolution_clock::now(); } ~Timer() { TimePoint stop = std::chrono::high_resolution_clock::now(); if (name_.isEmpty()) debug_.nospace().noquote() << "Timer: " << format(stop - start_).c_str(); else debug_.nospace().noquote() << "Timer [" << name_ << "]: " << format(stop - start_).c_str(); } private: using TimePoint = std::chrono::time_point; QDebug debug_; QString name_; TimePoint start_; }; using MsTimer = Timer; } // namespace SpeexWebRTCTest #define TIMER(debug) MsTimer timer(debug, __func__); #endif //_SPEEX_WEBRTC_TEST_TIMER_H_