[英]boost::chrono readable duration
我一直在研究一個使用boost chrono在微秒內運行的分析器,它適用於小功能,但我傾向於使用更大的功能獲得非常高的數字。
想象一下以下場景
boost::chrono::duration<long long, boost::micro> us1(300);
boost::chrono::duration<long long, boost::micro> us2(200000);
std::cout << boost::chrono::duration_short << "us1: " << us1 << ", us2: " << us2;
輸出將如下所示
us1:300 us,us2:200000 us
這可能變得難以量化,所以我想知道是否有一種方法可以舍入到更高的單位,以便輸出看起來像
us1:300 us,us2:200 ms
感謝大家,我解決了這個問題:
const std::string readableDuration(const boost::chrono::duration<long long, boost::micro>& duration)
{
std::stringstream stream;
int digits = calcNumDigits(duration.count());
stream << boost::chrono::duration_short;
if (digits <= 3) {
stream << duration;
} else if ((digits > 3) && (digits <= 6)) {
stream << boost::chrono::duration_cast<boost::chrono::milliseconds>(duration);
} else if ((digits > 6) && (digits <= 9)) {
stream << boost::chrono::duration_cast<boost::chrono::seconds>(duration);
} else if (digits > 9)
{
stream << boost::chrono::duration_cast<boost::chrono::minutes>(duration);
}
return stream.str();
}
可能值得使流靜態,但你得到了一般的想法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.