add the fixed snprintf serialiser

Run on (4 X 3500 MHz CPU s)
CPU Caches:
  L1 Data 32K (x4)
  L1 Instruction 32K (x4)
  L2 Unified 262K (x4)
  L3 Unified 6291K (x1)
-------------------------------------------------------------------------------------------------------------
Benchmark                                                                   Time             CPU   Iterations
-------------------------------------------------------------------------------------------------------------
RandFloatStrs/double_from_string_sstream                                  969 ns          977 ns       640000
RandFloatStrs/double_from_string_strtod                                   274 ns          270 ns      2488889
RandFloatStrs/double_from_string_strtod_fixed                             273 ns          273 ns      2635294
RandFloatStrs/double_from_string_strtod_fixed_const_ref                   274 ns          276 ns      2488889
RandFloatStrs/double_from_string_std_from_chars                           193 ns          193 ns      3733333
RandFloatCommaStrs/double_from_string_strtod_fixed_comma_ref              273 ns          267 ns      2635294
RandFloatCommaStrs/double_from_string_strtod_fixed_comma_const_ref        273 ns          273 ns      2635294
RandFloats/string_from_double_sstream                                    1323 ns         1311 ns       560000
RandFloats/string_from_double_sstream_cached                             1074 ns         1074 ns       640000
RandFloats/string_from_double_snprintf                                    519 ns          516 ns      1000000
RandFloats/string_from_double_snprintf_fixed                              517 ns          516 ns      1000000
RandFloats/string_from_double_std_to_chars                                118 ns          117 ns      5600000
RandFloatsComma/string_from_double_snprintf_fixed_comma                   520 ns          516 ns      1120000
This commit is contained in:
JCrawfy 2020-02-29 23:09:07 +13:00
parent 7ba36b5e73
commit a5aca5c212

View File

@ -94,7 +94,34 @@ public:
}
};
class number_serialiser_mk2
{
bool should_convert_to_comma;
public:
explicit number_serialiser_mk2()
: should_convert_to_comma(std::use_facet<std::numpunct<char>>(std::locale{}).decimal_point() == ',')
{
}
std::string serialise(double d)
{
char buf[16];
int len = snprintf(buf, sizeof(buf), "%16f", d);
if (should_convert_to_comma)
{
auto decimal = std::find(buf, buf + len, ',');
if (decimal != buf + len)
{
*decimal = '.';
}
}
return std::string(buf, len);
}
};
using RandFloats = RandomFloats<true>;
using RandFloatsComma = RandomFloats<false>;
} // namespace
BENCHMARK_F(RandFloats, string_from_double_sstream)
@ -131,6 +158,17 @@ BENCHMARK_F(RandFloats, string_from_double_snprintf)
}
}
BENCHMARK_F(RandFloats, string_from_double_snprintf_fixed)
(benchmark::State &state)
{
number_serialiser_mk2 ser;
while (state.KeepRunning())
{
benchmark::DoNotOptimize(
ser.serialise(get_rand()));
}
}
// locale names are different between OS's, and std::from_chars is only complete in MSVC
#ifdef _MSC_VER
@ -148,4 +186,15 @@ BENCHMARK_F(RandFloats, string_from_double_std_to_chars)
}
}
BENCHMARK_F(RandFloatsComma, string_from_double_snprintf_fixed_comma)
(benchmark::State &state)
{
number_serialiser_mk2 ser;
while (state.KeepRunning())
{
benchmark::DoNotOptimize(
ser.serialise(get_rand()));
}
}
#endif