-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Add benchmarks for copy algorithms #127328
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ae52f4
[libc++] Add benchmarks for copy algorithms
ldionne 76b025a
Guard for lack of output_iterator-nesss
ldionne b212204
Use ranges CPOs directly
ldionne d672c6d
Fix uses of ClobberMemory
ldionne d34de3e
Implement consensus with Nikolas
ldionne bf61250
Use rng::copy instead of ranges::copy -- that way the results align b…
ldionne 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
libcxx/test/benchmarks/algorithms/modifying/copy.bench.cpp
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,83 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <deque> | ||
#include <iterator> | ||
#include <list> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "benchmark/benchmark.h" | ||
#include "../../GenerateInput.h" | ||
#include "test_macros.h" | ||
|
||
int main(int argc, char** argv) { | ||
auto std_copy = [](auto first, auto last, auto out) { return std::copy(first, last, out); }; | ||
|
||
// {std,ranges}::copy(normal container) | ||
{ | ||
auto bm = []<class Container>(std::string name, auto copy) { | ||
benchmark::RegisterBenchmark(name, [copy](auto& st) { | ||
std::size_t const n = st.range(0); | ||
using ValueType = typename Container::value_type; | ||
Container c; | ||
std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); }); | ||
|
||
std::vector<ValueType> out(n); | ||
|
||
for ([[maybe_unused]] auto _ : st) { | ||
benchmark::DoNotOptimize(c); | ||
benchmark::DoNotOptimize(out); | ||
auto result = copy(c.begin(), c.end(), out.begin()); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
})->Range(8, 1 << 20); | ||
}; | ||
bm.operator()<std::vector<int>>("std::copy(vector<int>)", std_copy); | ||
bm.operator()<std::deque<int>>("std::copy(deque<int>)", std_copy); | ||
bm.operator()<std::list<int>>("std::copy(list<int>)", std_copy); | ||
bm.operator()<std::vector<int>>("rng::copy(vector<int>)", std::ranges::copy); | ||
bm.operator()<std::deque<int>>("rng::copy(deque<int>)", std::ranges::copy); | ||
bm.operator()<std::list<int>>("rng::copy(list<int>)", std::ranges::copy); | ||
} | ||
|
||
// {std,ranges}::copy(vector<bool>) | ||
{ | ||
auto bm = []<bool Aligned>(std::string name, auto copy) { | ||
benchmark::RegisterBenchmark(name, [copy](auto& st) { | ||
std::size_t const n = st.range(0); | ||
std::vector<bool> in(n, true); | ||
std::vector<bool> out(Aligned ? n : n + 8); | ||
auto first = in.begin(); | ||
auto last = in.end(); | ||
auto dst = Aligned ? out.begin() : out.begin() + 4; | ||
for ([[maybe_unused]] auto _ : st) { | ||
benchmark::DoNotOptimize(in); | ||
benchmark::DoNotOptimize(out); | ||
auto result = copy(first, last, dst); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
})->Range(64, 1 << 20); | ||
}; | ||
bm.operator()<true>("std::copy(vector<bool>) (aligned)", std_copy); | ||
bm.operator()<false>("std::copy(vector<bool>) (unaligned)", std_copy); | ||
#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23 | ||
bm.operator()<true>("rng::copy(vector<bool>) (aligned)", std::ranges::copy); | ||
bm.operator()<false>("rng::copy(vector<bool>) (unaligned)", std::ranges::copy); | ||
#endif | ||
} | ||
|
||
benchmark::Initialize(&argc, argv); | ||
benchmark::RunSpecifiedBenchmarks(); | ||
benchmark::Shutdown(); | ||
return 0; | ||
} |
84 changes: 84 additions & 0 deletions
84
libcxx/test/benchmarks/algorithms/modifying/copy_backward.bench.cpp
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 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <deque> | ||
#include <iterator> | ||
#include <list> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "benchmark/benchmark.h" | ||
#include "../../GenerateInput.h" | ||
#include "test_macros.h" | ||
|
||
int main(int argc, char** argv) { | ||
auto std_copy_backward = [](auto first, auto last, auto out) { return std::copy_backward(first, last, out); }; | ||
|
||
// {std,ranges}::copy_n(normal container) | ||
{ | ||
auto bm = []<class Container>(std::string name, auto copy_backward) { | ||
benchmark::RegisterBenchmark(name, [copy_backward](auto& st) { | ||
std::size_t const n = st.range(0); | ||
using ValueType = typename Container::value_type; | ||
Container c; | ||
std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); }); | ||
|
||
std::vector<ValueType> out(n); | ||
|
||
for ([[maybe_unused]] auto _ : st) { | ||
benchmark::DoNotOptimize(c); | ||
benchmark::DoNotOptimize(out); | ||
auto result = copy_backward(c.begin(), c.end(), out.end()); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
})->Range(8, 1 << 20); | ||
}; | ||
bm.operator()<std::vector<int>>("std::copy_backward(vector<int>)", std_copy_backward); | ||
bm.operator()<std::deque<int>>("std::copy_backward(deque<int>)", std_copy_backward); | ||
bm.operator()<std::list<int>>("std::copy_backward(list<int>)", std_copy_backward); | ||
bm.operator()<std::vector<int>>("rng::copy_backward(vector<int>)", std::ranges::copy_backward); | ||
bm.operator()<std::deque<int>>("rng::copy_backward(deque<int>)", std::ranges::copy_backward); | ||
bm.operator()<std::list<int>>("rng::copy_backward(list<int>)", std::ranges::copy_backward); | ||
} | ||
|
||
// {std,ranges}::copy_n(vector<bool>) | ||
{ | ||
auto bm = []<bool Aligned>(std::string name, auto copy_backward) { | ||
benchmark::RegisterBenchmark(name, [copy_backward](auto& st) { | ||
std::size_t const n = st.range(0); | ||
std::vector<bool> in(n, true); | ||
std::vector<bool> out(Aligned ? n : n + 8); | ||
benchmark::DoNotOptimize(&in); | ||
auto first = in.begin(); | ||
auto last = in.end(); | ||
auto dst = Aligned ? out.end() : out.end() - 4; | ||
for ([[maybe_unused]] auto _ : st) { | ||
benchmark::DoNotOptimize(in); | ||
benchmark::DoNotOptimize(out); | ||
auto result = copy_backward(first, last, dst); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
})->Range(64, 1 << 20); | ||
}; | ||
bm.operator()<true>("std::copy_backward(vector<bool>) (aligned)", std_copy_backward); | ||
bm.operator()<false>("std::copy_backward(vector<bool>) (unaligned)", std_copy_backward); | ||
#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23 | ||
bm.operator()<true>("rng::copy_backward(vector<bool>) (aligned)", std::ranges::copy_backward); | ||
bm.operator()<false>("rng::copy_backward(vector<bool>) (unaligned)", std::ranges::copy_backward); | ||
#endif | ||
} | ||
|
||
benchmark::Initialize(&argc, argv); | ||
benchmark::RunSpecifiedBenchmarks(); | ||
benchmark::Shutdown(); | ||
return 0; | ||
} |
Oops, something went wrong.
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.