-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add std::fill and std::fill_n benchmarks
#5400
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 15 commits into
microsoft:main
from
barcharcraz:dev/chbarto/fill_benchmarks
Apr 22, 2025
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c8e353f
add std::fill benchmarks
ce5616c
clang-format
5c11eaf
newline
430212d
use vectors instead of unique_ptrs
0037d71
formatting
c0b686e
Add banner.
StephanTLavavej 0b252ac
Update headers.
StephanTLavavej 0caea03
Drop unnamed namespace.
StephanTLavavej a42c08b
Adjust newlines.
StephanTLavavej 6c2aa21
Call benchmarks in definition order.
StephanTLavavej 18017e4
Drop const_size benchmarks.
StephanTLavavej d43502c
Test std algorithms with Value 1.
StephanTLavavej 429349f
Consistently vary the allocator.
StephanTLavavej d6d9053
Respect and fear std::memset.
StephanTLavavej 6b2335a
Always use `not_highly_aligned_allocator`.
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,127 @@ | ||
| #include <benchmark/benchmark.h> | ||
| #include <cstring> | ||
| #include <memory> | ||
| #include <ranges> | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #include <vector> | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| namespace { | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| template <typename Contained, Contained Value> | ||
| void handwritten_loop(benchmark::State& state) { | ||
| const size_t r0 = static_cast<size_t>(state.range(0)); | ||
| std::unique_ptr<Contained[]> buffer(new Contained[r0]); | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| Contained* ptr = buffer.get(); | ||
| const Contained* const ptr_end = ptr + r0; | ||
| while (ptr != ptr_end) { | ||
| *ptr++ = Value; | ||
| } | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| } | ||
| } | ||
|
|
||
| template <typename Contained, Contained Value, size_t Size> | ||
| void handwritten_loop_const_size(benchmark::State& state) { | ||
| std::unique_ptr<Contained[]> buffer(new Contained[Size]); | ||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| Contained* ptr = buffer.get(); | ||
| const Contained* const ptr_end = ptr + Size; | ||
| while (ptr != ptr_end) { | ||
| *ptr++ = Value; | ||
| } | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| } | ||
| } | ||
|
|
||
| template <typename Contained, Contained Value> | ||
| void handwritten_loop_n(benchmark::State& state) { | ||
| const size_t r0 = static_cast<size_t>(state.range(0)); | ||
| std::unique_ptr<Contained[]> buffer(new Contained[r0]); | ||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| Contained* ptr = buffer.get(); | ||
| for (size_t idx = 0; idx < r0; ++idx) { | ||
| ptr[idx] = Value; | ||
| } | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| } | ||
| } | ||
| template <typename Contained, Contained Value, size_t Size> | ||
| void handwritten_loop_n_const_size(benchmark::State& state) { | ||
| std::unique_ptr<Contained[]> buffer(new Contained[Size]); | ||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| Contained* ptr = buffer.get(); | ||
| for (size_t idx = 0; idx < Size; ++idx) { | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ptr[idx] = Value; | ||
| } | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| } | ||
| } | ||
|
|
||
| template <typename Contained, Contained Value> | ||
| void memset_call(benchmark::State& state) { | ||
| const size_t r0 = static_cast<size_t>(state.range(0)); | ||
| std::unique_ptr<Contained[]> buffer(new Contained[r0]); | ||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| Contained* ptr = buffer.get(); | ||
| std::memset(ptr, Value, r0); | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| benchmark::DoNotOptimize(buffer.get()); | ||
| } | ||
| } | ||
| template <typename Contained, Contained Value> | ||
| void std_fill_call(benchmark::State& state) { | ||
| const size_t r0 = static_cast<size_t>(state.range(0)); | ||
| std::unique_ptr<Contained[]> buffer(new Contained[r0]); | ||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| auto begin_it = buffer.get(); | ||
| auto end_it = buffer.get() + r0; | ||
| std::fill(begin_it, end_it, Value); | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| benchmark::DoNotOptimize(buffer.get()); | ||
| } | ||
| } | ||
|
|
||
| template <typename Contained, Contained Value> | ||
| void std_fill_n_call(benchmark::State& state) { | ||
| const size_t r0 = static_cast<size_t>(state.range(0)); | ||
| std::unique_ptr<Contained[]> buffer(new Contained[r0]); | ||
| for ([[maybe_unused]] auto _ : state) { | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| auto begin_it = buffer.get(); | ||
| std::fill_n(begin_it, r0, Value); | ||
| benchmark::DoNotOptimize(buffer.get()); | ||
| } | ||
| } | ||
| } // namespace | ||
|
|
||
| BENCHMARK(handwritten_loop<char, 0>)->Range(0, 1 << 18); | ||
| BENCHMARK(handwritten_loop<char, 1>)->Range(0, 1 << 18); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 0>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 1>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 8>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 64>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 512>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 1000>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 4096>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 32768>); | ||
| BENCHMARK(handwritten_loop_const_size<char, 0, 262144>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 0>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 1>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 8>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 64>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 512>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 1000>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 4096>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 32768>); | ||
| BENCHMARK(handwritten_loop_n_const_size<char, 0, 262144>); | ||
| BENCHMARK(handwritten_loop_n<char, 0>)->Range(0, 1 << 18); | ||
| BENCHMARK(handwritten_loop_n<char, 1>)->Range(0, 1 << 18); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| BENCHMARK(memset_call<char, 0>)->Range(0, 1 << 18); | ||
| BENCHMARK(memset_call<char, 1>)->Range(0, 1 << 18); | ||
| BENCHMARK(std_fill_call<char, 0>)->Range(0, 1 << 18); | ||
| BENCHMARK(std_fill_n_call<char, 0>)->Range(0, 1 << 18); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| 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.