Skip to content

Commit ead91b5

Browse files
committed
[libc++][ranges] add benchmark for 'ranges::shift_left'
1 parent 7cde586 commit ead91b5

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

libcxx/benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ set(BENCHMARK_TESTS
191191
algorithms/ranges_make_heap_then_sort_heap.bench.cpp
192192
algorithms/ranges_pop_heap.bench.cpp
193193
algorithms/ranges_push_heap.bench.cpp
194+
algorithms/ranges_shift_left.bench.cpp
194195
algorithms/ranges_sort.bench.cpp
195196
algorithms/ranges_sort_heap.bench.cpp
196197
algorithms/ranges_stable_sort.bench.cpp
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <algorithm>
10+
#include <benchmark/benchmark.h>
11+
#include <iterator>
12+
#include <vector>
13+
14+
#include "test_iterators.h"
15+
16+
static void bm_shift_left_random_access_range_with_sized_sentinel(benchmark::State& state) {
17+
std::vector<int> a(state.range(), 1);
18+
19+
for (auto _ : state) {
20+
benchmark::DoNotOptimize(a);
21+
22+
auto begin = random_access_iterator(a.data());
23+
auto end = random_access_iterator(a.data() + a.size());
24+
25+
static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
26+
static_assert(std::random_access_iterator<decltype(begin)>);
27+
28+
benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
29+
}
30+
}
31+
BENCHMARK(bm_shift_left_random_access_range_with_sized_sentinel)->RangeMultiplier(16)->Range(16, 16 << 20);
32+
33+
static void bm_shift_left_forward_range_with_sized_sentinel(benchmark::State& state) {
34+
std::vector<int> a(state.range(), 1);
35+
36+
for (auto _ : state) {
37+
benchmark::DoNotOptimize(a);
38+
39+
auto begin = forward_iterator(a.data());
40+
auto end = sized_sentinel(forward_iterator(a.data() + a.size()));
41+
42+
static_assert(std::sized_sentinel_for<decltype(end), decltype(begin)>);
43+
static_assert(!std::random_access_iterator<decltype(begin)>);
44+
45+
benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
46+
}
47+
}
48+
BENCHMARK(bm_shift_left_forward_range_with_sized_sentinel)->RangeMultiplier(16)->Range(16, 16 << 20);
49+
50+
static void bm_shift_left_forward_range(benchmark::State& state) {
51+
std::vector<int> a(state.range(), 1);
52+
53+
for (auto _ : state) {
54+
benchmark::DoNotOptimize(a);
55+
56+
auto begin = forward_iterator(a.data());
57+
auto end = forward_iterator(a.data() + a.size());
58+
59+
static_assert(!std::sized_sentinel_for<decltype(end), decltype(begin)>);
60+
static_assert(!std::random_access_iterator<decltype(begin)>);
61+
62+
benchmark::DoNotOptimize(std::ranges::shift_left(begin, end, a.size() / 2));
63+
}
64+
}
65+
BENCHMARK(bm_shift_left_forward_range)->RangeMultiplier(16)->Range(16, 16 << 20);
66+
67+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)