Add memory size pretty printing

This commit is contained in:
Nikita Lisitsa 2021-03-13 18:46:09 +03:00
parent b4731b77df
commit 2eab5b7b9a

View file

@ -2,6 +2,7 @@
#include <chrono>
#include <iostream>
#include <iomanip>
namespace psemek::util
{
@ -27,6 +28,23 @@ namespace psemek::util
return o;
}
struct pretty_print_memsize_wrapper
{
std::size_t value;
};
inline std::ostream & operator << (std::ostream & o, pretty_print_memsize_wrapper m)
{
if (m.value < (1 << 10))
return o << m.value << " b";
else if (m.value < (1 << 20))
return o << std::setprecision(3) << (m.value * 1.f / (1 << 10)) << " Kb";
else if (m.value < (1 << 30))
return o << std::setprecision(3) << (m.value * 1.f / (1 << 20)) << " Mb";
else
return o << std::setprecision(3) << (m.value * 1.f / (1 << 30)) << " Gb";
}
}
template <typename Rep, typename Period, typename UpTo>
@ -41,4 +59,9 @@ namespace psemek::util
return pretty(d, std::chrono::seconds{1});
}
inline auto pretty_memsize(std::size_t value)
{
return detail::pretty_print_memsize_wrapper{value};
}
}