Skip to content

[libc++] Add a benchmark for std::reverse #125262

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 1 commit into from
Feb 3, 2025

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Jan 31, 2025

No description provided.

@ldionne ldionne requested a review from a team as a code owner January 31, 2025 17:51
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jan 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 31, 2025

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/125262.diff

1 Files Affected:

  • (added) libcxx/test/benchmarks/algorithms/reverse.bench.cpp (+48)
diff --git a/libcxx/test/benchmarks/algorithms/reverse.bench.cpp b/libcxx/test/benchmarks/algorithms/reverse.bench.cpp
new file mode 100644
index 000000000000000..2d8dd819ac24c23
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/reverse.bench.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// 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, c++20
+
+#include <algorithm>
+#include <iterator>
+#include <string>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+#include "../GenerateInput.h"
+
+template <class T>
+static void bm_reverse(benchmark::State& state) {
+  std::size_t const n = state.range();
+  std::vector<T> vec;
+  std::generate_n(std::back_inserter(vec), n, [] { return Generate<T>::cheap(); });
+  for (auto _ : state) {
+    std::reverse(vec.begin(), vec.end());
+    benchmark::DoNotOptimize(vec);
+  }
+}
+BENCHMARK(bm_reverse<int>)->Name("std::reverse(vector<int>)")->DenseRange(1, 8)->Range(16, 1 << 20);
+BENCHMARK(bm_reverse<std::string>)->Name("std::reverse(vector<string>)")->DenseRange(1, 8)->Range(16, 1 << 20);
+
+template <class T>
+static void bm_ranges_reverse(benchmark::State& state) {
+  std::size_t const n = state.range();
+  std::vector<T> vec;
+  std::generate_n(std::back_inserter(vec), n, [] { return Generate<T>::cheap(); });
+  for (auto _ : state) {
+    std::ranges::reverse(vec.begin(), vec.end());
+    benchmark::DoNotOptimize(vec);
+  }
+}
+BENCHMARK(bm_ranges_reverse<int>)->Name("ranges::reverse(vector<int>)")->DenseRange(1, 8)->Range(16, 1 << 20);
+BENCHMARK(bm_ranges_reverse<std::string>)
+    ->Name("ranges::reverse(vector<string>)")
+    ->DenseRange(1, 8)
+    ->Range(16, 1 << 20);
+
+BENCHMARK_MAIN();

@ldionne ldionne merged commit c7c7eab into llvm:main Feb 3, 2025
80 checks passed
@ldionne ldionne deleted the review/add-reverse-benchmark branch February 3, 2025 17:10
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants