Skip to content

Commit c23c7c0

Browse files
committed
Use ValueType instead of int
1 parent 85293ea commit c23c7c0

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

libcxx/test/benchmarks/algorithms/nonmodifying/contains.bench.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vector>
1717

1818
#include <benchmark/benchmark.h>
19+
#include "../../GenerateInput.h"
1920

2021
int main(int argc, char** argv) {
2122
// Benchmark ranges::contains where we bail out early (after visiting 25% of the elements).
@@ -25,14 +26,18 @@ int main(int argc, char** argv) {
2526
name,
2627
[](auto& st) {
2728
std::size_t const size = st.range(0);
28-
Container c(size, 1);
29-
*std::next(c.begin(), size / 4) = 42; // bail out after checking 25% of values
29+
using ValueType = typename Container::value_type;
30+
ValueType x = Generate<ValueType>::random();
31+
ValueType y = random_different_from({x});
32+
Container c(size, x);
33+
*std::next(c.begin(), size / 4) = y; // bail out after checking 25% of values
3034
auto first = c.begin();
3135
auto last = c.end();
3236

3337
for (auto _ : st) {
3438
benchmark::DoNotOptimize(c);
35-
auto result = std::ranges::contains(first, last, 42);
39+
benchmark::DoNotOptimize(y);
40+
auto result = std::ranges::contains(first, last, y);
3641
benchmark::DoNotOptimize(result);
3742
}
3843
})
@@ -53,13 +58,17 @@ int main(int argc, char** argv) {
5358
name,
5459
[](auto& st) {
5560
std::size_t const size = st.range(0);
56-
Container c(size, 1);
61+
using ValueType = typename Container::value_type;
62+
ValueType x = Generate<ValueType>::random();
63+
ValueType y = random_different_from({x});
64+
Container c(size, x);
5765
auto first = c.begin();
5866
auto last = c.end();
5967

6068
for (auto _ : st) {
6169
benchmark::DoNotOptimize(c);
62-
auto result = std::ranges::contains(first, last, 42);
70+
benchmark::DoNotOptimize(y);
71+
auto result = std::ranges::contains(first, last, y);
6372
benchmark::DoNotOptimize(result);
6473
}
6574
})

libcxx/test/benchmarks/algorithms/nonmodifying/contains_subrange.bench.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vector>
1717

1818
#include <benchmark/benchmark.h>
19+
#include "../../GenerateInput.h"
1920

2021
int main(int argc, char** argv) {
2122
// Benchmark ranges::contains_subrange where we find our target starting at 25% of the elements
@@ -25,8 +26,11 @@ int main(int argc, char** argv) {
2526
name,
2627
[](auto& st) {
2728
std::size_t const size = st.range(0);
28-
Container c(size, 1);
29-
Container subrange(size / 10, 42); // subrange of length 10% of the full range
29+
using ValueType = typename Container::value_type;
30+
ValueType x = Generate<ValueType>::random();
31+
ValueType y = random_different_from({x});
32+
Container c(size, x);
33+
Container subrange(size / 10, y); // subrange of length 10% of the full range
3034

3135
// At 25% of the range, put the subrange we're going to find
3236
std::ranges::copy(subrange, std::next(c.begin(), c.size() / 4));
@@ -55,8 +59,11 @@ int main(int argc, char** argv) {
5559
name,
5660
[](auto& st) {
5761
std::size_t const size = st.range(0);
58-
Container c(size, 1);
59-
Container subrange(size / 10, 42); // subrange of length 10% of the full range, but we'll never find it
62+
using ValueType = typename Container::value_type;
63+
ValueType x = Generate<ValueType>::random();
64+
ValueType y = random_different_from({x});
65+
Container c(size, x);
66+
Container subrange(size / 10, y); // subrange of length 10% of the full range, but we'll never find it
6067

6168
for (auto _ : st) {
6269
benchmark::DoNotOptimize(c);

0 commit comments

Comments
 (0)