Commit d7041799 by Dominic Hamon Committed by GitHub

Allow the definition of 1k to be flexible. (#438)

When generating a human-readable number for user counters, we don't generally expect 1k to be 1024. This is the default due to the more general purpose string utility. Fixes #437
parent c7192c8a
...@@ -148,7 +148,7 @@ void ConsoleReporter::PrintRunData(const Run& result) { ...@@ -148,7 +148,7 @@ void ConsoleReporter::PrintRunData(const Run& result) {
} }
for (auto& c : result.counters) { for (auto& c : result.counters) {
auto const& s = HumanReadableNumber(c.second.value); auto const& s = HumanReadableNumber(c.second.value, 1000);
if (output_options_ & OO_Tabular) { if (output_options_ & OO_Tabular) {
if (c.second.flags & Counter::kIsRate) { if (c.second.flags & Counter::kIsRate) {
printer(Out, COLOR_DEFAULT, " %8s/s", s.c_str()); printer(Out, COLOR_DEFAULT, " %8s/s", s.c_str());
......
...@@ -27,8 +27,6 @@ static_assert(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits), ...@@ -27,8 +27,6 @@ static_assert(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits),
static const int64_t kUnitsSize = arraysize(kBigSIUnits); static const int64_t kUnitsSize = arraysize(kBigSIUnits);
} // end anonymous namespace
void ToExponentAndMantissa(double val, double thresh, int precision, void ToExponentAndMantissa(double val, double thresh, int precision,
double one_k, std::string* mantissa, double one_k, std::string* mantissa,
int64_t* exponent) { int64_t* exponent) {
...@@ -100,14 +98,16 @@ std::string ExponentToPrefix(int64_t exponent, bool iec) { ...@@ -100,14 +98,16 @@ std::string ExponentToPrefix(int64_t exponent, bool iec) {
} }
std::string ToBinaryStringFullySpecified(double value, double threshold, std::string ToBinaryStringFullySpecified(double value, double threshold,
int precision) { int precision, double one_k = 1024.0) {
std::string mantissa; std::string mantissa;
int64_t exponent; int64_t exponent;
ToExponentAndMantissa(value, threshold, precision, 1024.0, &mantissa, ToExponentAndMantissa(value, threshold, precision, one_k, &mantissa,
&exponent); &exponent);
return mantissa + ExponentToPrefix(exponent, false); return mantissa + ExponentToPrefix(exponent, false);
} }
} // end namespace
void AppendHumanReadable(int n, std::string* str) { void AppendHumanReadable(int n, std::string* str) {
std::stringstream ss; std::stringstream ss;
// Round down to the nearest SI prefix. // Round down to the nearest SI prefix.
...@@ -115,11 +115,11 @@ void AppendHumanReadable(int n, std::string* str) { ...@@ -115,11 +115,11 @@ void AppendHumanReadable(int n, std::string* str) {
*str += ss.str(); *str += ss.str();
} }
std::string HumanReadableNumber(double n) { std::string HumanReadableNumber(double n, double one_k) {
// 1.1 means that figures up to 1.1k should be shown with the next unit down; // 1.1 means that figures up to 1.1k should be shown with the next unit down;
// this softens edge effects. // this softens edge effects.
// 1 means that we should show one decimal place of precision. // 1 means that we should show one decimal place of precision.
return ToBinaryStringFullySpecified(n, 1.1, 1); return ToBinaryStringFullySpecified(n, 1.1, 1, one_k);
} }
std::string StringPrintFImp(const char* msg, va_list args) { std::string StringPrintFImp(const char* msg, va_list args) {
......
...@@ -10,7 +10,7 @@ namespace benchmark { ...@@ -10,7 +10,7 @@ namespace benchmark {
void AppendHumanReadable(int n, std::string* str); void AppendHumanReadable(int n, std::string* str);
std::string HumanReadableNumber(double n); std::string HumanReadableNumber(double n, double one_k = 1024.0);
std::string StringPrintF(const char* format, ...); std::string StringPrintF(const char* format, ...);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment