I have been working on a profiler that times functions in microseconds using boost chrono and it works well for small functions but I tend to get very high number with bigger ones.
Imagine the following scenario
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;
The output will look like this
us1: 300 us , us2: 200000 us
which can become hard to quantify so i'd like to know if there is a way to round to the higher unit so that the output can look like
us1: 300 us , us2: 200 ms
Thanks to everyone, I solved it that way:
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();
}
Might be worth making stream static but you get the general idea
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.