diff --git a/benchmarks/microbenchmarks/double_to_string.cpp b/benchmarks/microbenchmarks/double_to_string.cpp index 01efa9ef..1182219c 100644 --- a/benchmarks/microbenchmarks/double_to_string.cpp +++ b/benchmarks/microbenchmarks/double_to_string.cpp @@ -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::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; +using RandFloatsComma = RandomFloats; } // 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 \ No newline at end of file