Skip to content

[libc++] Add remaining benchmarks from [alg.modifying.operations] #127354

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 15 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libcxx/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ module std [system] {
}
module ranges_remove {
header "__algorithm/ranges_remove.h"
export std.ranges.subrange // return type
}
module ranges_replace_copy_if {
header "__algorithm/ranges_replace_copy_if.h"
Expand All @@ -732,7 +733,7 @@ module std [system] {
}
module ranges_rotate_copy {
header "__algorithm/ranges_rotate_copy.h"
export std.algorithm.in_out_result
export std.ranges.subrange // return type
}
module ranges_rotate { header "__algorithm/ranges_rotate.h" }
module ranges_sample { header "__algorithm/ranges_sample.h" }
Expand Down
9 changes: 9 additions & 0 deletions libcxx/test/benchmarks/GenerateInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,13 @@ struct Generate<std::string> {
}
};

template <class T>
T random_different_from(std::initializer_list<T> others) {
T value;
do {
value = Generate<T>::random();
} while (std::find(others.begin(), others.end(), value) != others.end());
return value;
}

#endif // BENCHMARK_GENERATE_INPUT_H
51 changes: 0 additions & 51 deletions libcxx/test/benchmarks/algorithms/fill.bench.cpp

This file was deleted.

83 changes: 83 additions & 0 deletions libcxx/test/benchmarks/algorithms/modifying/fill.bench.cpp
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_fill = [](auto first, auto last, auto const& value) { return std::fill(first, last, value); };

// {std,ranges}::fill(normal container)
{
auto bm = []<class Container>(std::string name, auto fill) {
benchmark::RegisterBenchmark(
name,
[fill](auto& st) {
std::size_t const size = st.range(0);
using ValueType = typename Container::value_type;
ValueType x = Generate<ValueType>::random();
Container c(size, x);

for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
benchmark::DoNotOptimize(x);
fill(c.begin(), c.end(), x);
benchmark::DoNotOptimize(c);
}
})
->Arg(32)
->Arg(50) // non power-of-two
->Arg(1024)
->Arg(8192);
};
bm.operator()<std::vector<int>>("std::fill(vector<int>)", std_fill);
bm.operator()<std::deque<int>>("std::fill(deque<int>)", std_fill);
bm.operator()<std::list<int>>("std::fill(list<int>)", std_fill);
bm.operator()<std::vector<int>>("rng::fill(vector<int>)", std::ranges::fill);
bm.operator()<std::deque<int>>("rng::fill(deque<int>)", std::ranges::fill);
bm.operator()<std::list<int>>("rng::fill(list<int>)", std::ranges::fill);
}

// {std,ranges}::fill(vector<bool>)
{
auto bm = [](std::string name, auto fill) {
benchmark::RegisterBenchmark(name, [fill](auto& st) {
std::size_t const size = st.range(0);
bool x = true;
std::vector<bool> c(size, x);

for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
benchmark::DoNotOptimize(x);
fill(c.begin(), c.end(), x);
benchmark::DoNotOptimize(c);
}
})->Range(64, 1 << 20);
};
bm("std::fill(vector<bool>)", std_fill);
#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23
bm("rng::fill(vector<bool>)", std::ranges::fill);
#endif
}

benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
83 changes: 83 additions & 0 deletions libcxx/test/benchmarks/algorithms/modifying/fill_n.bench.cpp
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_fill_n = [](auto out, auto n, auto const& value) { return std::fill_n(out, n, value); };

// {std,ranges}::fill_n(normal container)
{
auto bm = []<class Container>(std::string name, auto fill_n) {
benchmark::RegisterBenchmark(
name,
[fill_n](auto& st) {
std::size_t const size = st.range(0);
using ValueType = typename Container::value_type;
ValueType x = Generate<ValueType>::random();
Container c(size, x);

for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
benchmark::DoNotOptimize(x);
fill_n(c.begin(), size, x);
benchmark::DoNotOptimize(c);
}
})
->Arg(32)
->Arg(50) // non power-of-two
->Arg(1024)
->Arg(8192);
};
bm.operator()<std::vector<int>>("std::fill_n(vector<int>)", std_fill_n);
bm.operator()<std::deque<int>>("std::fill_n(deque<int>)", std_fill_n);
bm.operator()<std::list<int>>("std::fill_n(list<int>)", std_fill_n);
bm.operator()<std::vector<int>>("rng::fill_n(vector<int>)", std::ranges::fill_n);
bm.operator()<std::deque<int>>("rng::fill_n(deque<int>)", std::ranges::fill_n);
bm.operator()<std::list<int>>("rng::fill_n(list<int>)", std::ranges::fill_n);
}

// {std,ranges}::fill_n(vector<bool>)
{
auto bm = [](std::string name, auto fill_n) {
benchmark::RegisterBenchmark(name, [fill_n](auto& st) {
std::size_t const size = st.range(0);
bool x = true;
std::vector<bool> c(size, x);

for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
benchmark::DoNotOptimize(x);
fill_n(c.begin(), size, x);
benchmark::DoNotOptimize(c);
}
})->Range(64, 1 << 20);
};
bm("std::fill_n(vector<bool>)", std_fill_n);
#if TEST_STD_VER >= 23 // vector<bool>::iterator is not an output_iterator before C++23
bm("rng::fill_n(vector<bool>)", std::ranges::fill_n);
#endif
}

benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
62 changes: 62 additions & 0 deletions libcxx/test/benchmarks/algorithms/modifying/generate.bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// 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"

int main(int argc, char** argv) {
auto std_generate = [](auto first, auto last, auto f) { return std::generate(first, last, f); };

// {std,ranges}::generate
{
auto bm = []<class Container>(std::string name, auto generate) {
benchmark::RegisterBenchmark(
name,
[generate](auto& st) {
std::size_t const size = st.range(0);
Container c(size);
using ValueType = typename Container::value_type;
ValueType x = Generate<ValueType>::random();

for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
generate(c.begin(), c.end(), [&x] {
benchmark::DoNotOptimize(x);
return x;
});
benchmark::DoNotOptimize(c);
}
})
->Arg(32)
->Arg(50) // non power-of-two
->Arg(1024)
->Arg(8192);
};
bm.operator()<std::vector<int>>("std::generate(vector<int>)", std_generate);
bm.operator()<std::deque<int>>("std::generate(deque<int>)", std_generate);
bm.operator()<std::list<int>>("std::generate(list<int>)", std_generate);
bm.operator()<std::vector<int>>("rng::generate(vector<int>)", std::ranges::generate);
bm.operator()<std::deque<int>>("rng::generate(deque<int>)", std::ranges::generate);
bm.operator()<std::list<int>>("rng::generate(list<int>)", std::ranges::generate);
}

benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// 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"

int main(int argc, char** argv) {
auto std_generate_n = [](auto out, auto n, auto f) { return std::generate_n(out, n, f); };

// {std,ranges}::generate_n
{
auto bm = []<class Container>(std::string name, auto generate_n) {
benchmark::RegisterBenchmark(
name,
[generate_n](auto& st) {
std::size_t const size = st.range(0);
Container c(size);
using ValueType = typename Container::value_type;
ValueType x = Generate<ValueType>::random();

for ([[maybe_unused]] auto _ : st) {
benchmark::DoNotOptimize(c);
generate_n(c.begin(), size, [&x] {
benchmark::DoNotOptimize(x);
return x;
});
benchmark::DoNotOptimize(c);
}
})
->Arg(32)
->Arg(50) // non power-of-two
->Arg(1024)
->Arg(8192);
};
bm.operator()<std::vector<int>>("std::generate_n(vector<int>)", std_generate_n);
bm.operator()<std::deque<int>>("std::generate_n(deque<int>)", std_generate_n);
bm.operator()<std::list<int>>("std::generate_n(list<int>)", std_generate_n);
bm.operator()<std::vector<int>>("rng::generate_n(vector<int>)", std::ranges::generate_n);
bm.operator()<std::deque<int>>("rng::generate_n(deque<int>)", std::ranges::generate_n);
bm.operator()<std::list<int>>("rng::generate_n(list<int>)", std::ranges::generate_n);
}

benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
Loading