-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<charconv>: floating benchmark
#5700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StephanTLavavej
merged 12 commits into
microsoft:main
from
AlexGuteniev:charconv-float-benchmark
Oct 17, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5f342c3
Port benchmark from #1243 comment
AlexGuteniev 3e3cbba
use size, as we skip sometimes
AlexGuteniev c150dcb
no chrono
AlexGuteniev 018c9d6
Merge branch 'microsoft:main' into charconv-float-benchmark
AlexGuteniev 8ebcb93
Merge branch 'main' into charconv-float-benchmark
StephanTLavavej 35bded6
Adjust headers.
StephanTLavavej ffcc04e
Use bit_cast.
StephanTLavavej f3ba0d3
Use `auto... Args` and `BENCHMARK(X)->Name("Y")`.
StephanTLavavej dc5a34e
Use `consteval`, so we don't need to print FAIL.
StephanTLavavej 9a9a97e
Inline `BufSize` to avoid pointer arithmetic.
StephanTLavavej 51bd445
Reduce scope of `mt19937_64 mt64`.
StephanTLavavej 3734552
Reduce scope of `auto it`.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <benchmark/benchmark.h> | ||
| #include <bit> | ||
| #include <charconv> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <random> | ||
| #include <system_error> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| using namespace std; | ||
|
|
||
| void verify(const bool b) { | ||
| if (!b) { | ||
| puts("FAIL"); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| } | ||
|
|
||
| enum class RoundTrip { Sci, Fix, Gen, Hex, Lossy }; | ||
|
|
||
| consteval chars_format chars_format_from_RoundTrip(const RoundTrip rt) { | ||
| switch (rt) { | ||
| case RoundTrip::Sci: | ||
| return chars_format::scientific; | ||
| case RoundTrip::Fix: | ||
| return chars_format::fixed; | ||
| case RoundTrip::Gen: | ||
| return chars_format::general; | ||
| case RoundTrip::Hex: | ||
| return chars_format::hex; | ||
| case RoundTrip::Lossy: | ||
| default: | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| } | ||
|
|
||
| template <RoundTrip Rt, typename Floating, auto... Args> | ||
| void test_to_chars(benchmark::State& state) { | ||
| constexpr size_t n = 2'000'000; // how many floating-point values to test | ||
| vector<Floating> vec; | ||
| vec.reserve(n); | ||
|
|
||
| { | ||
| mt19937_64 mt64; | ||
| while (vec.size() < n) { | ||
| using Integral = conditional_t<sizeof(Floating) == 4, uint32_t, uint64_t>; | ||
| const Integral val = static_cast<Integral>(mt64()); | ||
| constexpr Integral inf_nan = sizeof(Floating) == 4 ? 0x7F800000U : 0x7FF0000000000000ULL; | ||
| if ((val & inf_nan) == inf_nan) { | ||
| continue; // skip INF/NAN | ||
| } | ||
| vec.push_back(bit_cast<Floating>(val)); | ||
| } | ||
| } | ||
|
|
||
| char buf[2'000]; // more than enough | ||
|
|
||
| { | ||
| auto it = vec.begin(); | ||
| for (auto _ : state) { | ||
| auto result = to_chars(buf, end(buf), *it, Args...); | ||
|
|
||
| benchmark::DoNotOptimize(result.ptr); | ||
| benchmark::DoNotOptimize(buf); | ||
|
|
||
| ++it; | ||
| if (it == vec.end()) { | ||
| it = vec.begin(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const auto& elem : vec) { | ||
| const auto result = to_chars(buf, end(buf), elem, Args...); | ||
| verify(result.ec == errc{}); | ||
|
|
||
| if constexpr (Rt == RoundTrip::Lossy) { | ||
| // skip lossy conversions | ||
| } else { | ||
| Floating round_trip; | ||
| const auto from_result = from_chars(buf, result.ptr, round_trip, chars_format_from_RoundTrip(Rt)); | ||
| verify(from_result.ec == errc{}); | ||
| verify(from_result.ptr == result.ptr); | ||
| verify(round_trip == elem); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(test_to_chars<RoundTrip::Gen, float>)->Name("STL_float_plain_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Gen, double>)->Name("STL_double_plain_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Sci, float, chars_format::scientific>)->Name("STL_float_scientific_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Sci, double, chars_format::scientific>)->Name("STL_double_scientific_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Fix, float, chars_format::fixed>)->Name("STL_float_fixed_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Fix, double, chars_format::fixed>)->Name("STL_double_fixed_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Gen, float, chars_format::general>)->Name("STL_float_general_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Gen, double, chars_format::general>)->Name("STL_double_general_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Hex, float, chars_format::hex>)->Name("STL_float_hex_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Hex, double, chars_format::hex>)->Name("STL_double_hex_shortest"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Sci, float, chars_format::scientific, 8>)->Name("STL_float_scientific_8"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Sci, double, chars_format::scientific, 16>)->Name("STL_double_scientific_16"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Lossy, float, chars_format::fixed, 6>)->Name("STL_float_fixed_6_lossy"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Lossy, double, chars_format::fixed, 6>)->Name("STL_double_fixed_6_lossy"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Gen, float, chars_format::general, 9>)->Name("STL_float_general_9"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Gen, double, chars_format::general, 17>)->Name("STL_double_general_17"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Hex, float, chars_format::hex, 6>)->Name("STL_float_hex_6"); | ||
| BENCHMARK(test_to_chars<RoundTrip::Hex, double, chars_format::hex, 13>)->Name("STL_double_hex_13"); | ||
|
|
||
| BENCHMARK_MAIN(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.