-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Use division by 100 in to_string for integers
#5691
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
Open
AlexGuteniev
wants to merge
7
commits into
microsoft:main
Choose a base branch
from
AlexGuteniev:integers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−12
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86958ab
benchmark
AlexGuteniev 8b6f863
100 branch in to_string impl
AlexGuteniev 84b3d1a
sort better
AlexGuteniev 08b83da
hack around some linker issue in C++14
AlexGuteniev 068234b
Merge branch 'microsoft:main' into integers
AlexGuteniev 5de9ae1
Merge branch 'microsoft:main' into integers
AlexGuteniev 74815fc
Merge branch 'microsoft:main' into integers
AlexGuteniev 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,84 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <benchmark/benchmark.h> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <random> | ||
| #include <type_traits> | ||
|
|
||
| using namespace std; | ||
|
|
||
| template <class T, double M, double S> | ||
| auto generate_array() { | ||
| array<T, 2000> a; | ||
|
|
||
| mt19937 gen; | ||
| lognormal_distribution<double> dis(M, S); | ||
| ranges::generate(a, [&] { return static_cast<T>(dis(gen)); }); | ||
|
|
||
| if constexpr (is_signed_v<T>) { | ||
| bernoulli_distribution b(0.5); | ||
| ranges::for_each(a, [&](T& v) { v *= (b(gen) ? -1 : 1); }); | ||
| } | ||
|
|
||
| return a; | ||
| } | ||
|
|
||
| template <class T, double M, double S> | ||
| void internal_integer_to_buff(benchmark::State& state) { | ||
| auto a = generate_array<T, M, S>(); | ||
|
|
||
| char buff[24]; | ||
|
|
||
| auto it = a.begin(); | ||
| for (auto _ : state) { | ||
| auto i = *it; | ||
| benchmark::DoNotOptimize(i); | ||
| auto s = std::_UIntegral_to_buff(buff, i); | ||
| benchmark::DoNotOptimize(s); | ||
|
|
||
| ++it; | ||
| if (it == a.end()) { | ||
| it = a.begin(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template <class T, double M, double S> | ||
| void integer_to_string(benchmark::State& state) { | ||
| auto a = generate_array<T, M, S>(); | ||
|
|
||
| auto it = a.begin(); | ||
| for (auto _ : state) { | ||
| auto i = *it; | ||
| benchmark::DoNotOptimize(i); | ||
| auto s = to_string(i); | ||
| benchmark::DoNotOptimize(s); | ||
|
|
||
| ++it; | ||
| if (it == a.end()) { | ||
| it = a.begin(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(internal_integer_to_buff<uint8_t, 2.5, 1.5>); | ||
| BENCHMARK(internal_integer_to_buff<uint16_t, 5.0, 3.0>); | ||
| BENCHMARK(internal_integer_to_buff<uint32_t, 10.0, 6.0>); | ||
| BENCHMARK(internal_integer_to_buff<uint64_t, 20.0, 12.0>); | ||
|
|
||
| BENCHMARK(integer_to_string<uint8_t, 2.5, 1.5>); | ||
| BENCHMARK(integer_to_string<uint16_t, 5.0, 3.0>); | ||
| BENCHMARK(integer_to_string<uint32_t, 10.0, 6.0>); | ||
| BENCHMARK(integer_to_string<uint64_t, 20.0, 12.0>); | ||
|
|
||
| BENCHMARK(integer_to_string<int8_t, 2.5, 1.5>); | ||
| BENCHMARK(integer_to_string<int16_t, 5.0, 3.0>); | ||
| BENCHMARK(integer_to_string<int32_t, 10.0, 6.0>); | ||
| BENCHMARK(integer_to_string<int64_t, 20.0, 12.0>); | ||
|
|
||
| BENCHMARK_MAIN(); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be further optimized to
still without much better results in the benchmark.