Skip to content

Commit af895f2

Browse files
authored
Improve existing vector<bool> benchmarks (#5732)
1 parent 21f58af commit af895f2

File tree

5 files changed

+92
-64
lines changed

5 files changed

+92
-64
lines changed

benchmarks/inc/utility.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ std::vector<Contained, Alloc<Contained>> random_vector(size_t n) {
2626

2727
return res;
2828
}
29+
30+
std::vector<bool> random_bool_vector(const size_t size) {
31+
std::mt19937 gen;
32+
std::bernoulli_distribution dist{0.5};
33+
std::vector<bool> result(size);
34+
std::generate(result.begin(), result.end(), [&] { return dist(gen); });
35+
return result;
36+
}

benchmarks/src/vector_bool_copy.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,93 @@
88
#include <random>
99
#include <vector>
1010

11-
using namespace std;
11+
#include "utility.hpp"
1212

13-
vector<bool> createRandomVector(const size_t size) {
14-
static mt19937 gen;
15-
vector<bool> result(size);
16-
generate_n(result.begin(), size, [] { return bernoulli_distribution{0.5}(gen); });
17-
return result;
18-
}
13+
using namespace std;
1914

2015
void copy_block_aligned(benchmark::State& state) {
21-
const auto size = static_cast<size_t>(state.range(0));
22-
const vector<bool> source = createRandomVector(size);
16+
const auto size = static_cast<size_t>(state.range(0));
17+
vector<bool> source = random_bool_vector(size);
2318
vector<bool> dest(size, false);
2419

2520
for (auto _ : state) {
21+
benchmark::DoNotOptimize(source);
2622
copy(source.cbegin(), source.cend(), dest.begin());
23+
benchmark::DoNotOptimize(dest);
2724
}
2825
}
2926

3027
void copy_source_misaligned(benchmark::State& state) {
31-
const auto size = static_cast<size_t>(state.range(0));
32-
const vector<bool> source = createRandomVector(size);
28+
const auto size = static_cast<size_t>(state.range(0));
29+
vector<bool> source = random_bool_vector(size);
3330
vector<bool> dest(size, false);
3431

3532
for (auto _ : state) {
33+
benchmark::DoNotOptimize(source);
3634
copy(source.cbegin() + 1, source.cend(), dest.begin());
35+
benchmark::DoNotOptimize(dest);
3736
}
3837
}
3938

4039
void copy_dest_misaligned(benchmark::State& state) {
41-
const auto size = static_cast<size_t>(state.range(0));
42-
const vector<bool> source = createRandomVector(size);
40+
const auto size = static_cast<size_t>(state.range(0));
41+
vector<bool> source = random_bool_vector(size);
4342
vector<bool> dest(size, false);
4443

4544
for (auto _ : state) {
45+
benchmark::DoNotOptimize(source);
4646
copy(source.cbegin(), source.cend() - 1, dest.begin() + 1);
47+
benchmark::DoNotOptimize(dest);
4748
}
4849
}
4950

5051
// Special benchmark for matching char alignment
5152
void copy_matching_alignment(benchmark::State& state) {
52-
const auto size = static_cast<size_t>(state.range(0));
53-
const vector<bool> source = createRandomVector(size);
53+
const auto size = static_cast<size_t>(state.range(0));
54+
vector<bool> source = random_bool_vector(size);
5455
vector<bool> dest(size, false);
5556

5657
for (auto _ : state) {
58+
benchmark::DoNotOptimize(source);
5759
copy(source.cbegin() + 5, source.cend(), dest.begin() + 5);
60+
benchmark::DoNotOptimize(dest);
5861
}
5962
}
6063

6164
// Special benchmarks for single block corner case
6265
void copy_both_single_blocks(benchmark::State& state) {
63-
const vector<bool> source = createRandomVector(50);
66+
vector<bool> source = random_bool_vector(50);
6467
vector<bool> dest(50, false);
6568

6669
const size_t length = 20;
6770
for (auto _ : state) {
71+
benchmark::DoNotOptimize(source);
6872
copy(source.cbegin() + 5, source.cbegin() + 5 + length, dest.begin() + 5);
73+
benchmark::DoNotOptimize(dest);
6974
}
7075
}
7176

7277
void copy_source_single_block(benchmark::State& state) {
73-
const vector<bool> source = createRandomVector(50);
78+
vector<bool> source = random_bool_vector(50);
7479
vector<bool> dest(50, false);
7580

7681
const size_t length = 20;
7782
for (auto _ : state) {
83+
benchmark::DoNotOptimize(source);
7884
copy(source.cbegin() + 5, source.cbegin() + 5 + length, dest.begin() + 25);
85+
benchmark::DoNotOptimize(dest);
7986
}
8087
}
8188

8289
void copy_dest_single_block(benchmark::State& state) {
83-
const vector<bool> source = createRandomVector(50);
90+
vector<bool> source = random_bool_vector(50);
8491
vector<bool> dest(50, false);
8592

8693
const size_t length = 20;
8794
for (auto _ : state) {
95+
benchmark::DoNotOptimize(source);
8896
copy(source.cbegin() + 25, source.cbegin() + 25 + length, dest.begin() + 5);
97+
benchmark::DoNotOptimize(dest);
8998
}
9099
}
91100

benchmarks/src/vector_bool_copy_n.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,93 @@
88
#include <random>
99
#include <vector>
1010

11-
using namespace std;
11+
#include "utility.hpp"
1212

13-
vector<bool> createRandomVector(const size_t size) {
14-
static mt19937 gen;
15-
vector<bool> result(size);
16-
generate_n(result.begin(), size, [] { return bernoulli_distribution{0.5}(gen); });
17-
return result;
18-
}
13+
using namespace std;
1914

2015
void copy_n_block_aligned(benchmark::State& state) {
21-
const auto size = static_cast<size_t>(state.range(0));
22-
const vector<bool> source = createRandomVector(size);
16+
const auto size = static_cast<size_t>(state.range(0));
17+
vector<bool> source = random_bool_vector(size);
2318
vector<bool> dest(size, false);
2419

2520
for (auto _ : state) {
21+
benchmark::DoNotOptimize(source);
2622
copy_n(source.cbegin(), size, dest.begin());
23+
benchmark::DoNotOptimize(dest);
2724
}
2825
}
2926

3027
void copy_n_source_misaligned(benchmark::State& state) {
31-
const auto size = static_cast<size_t>(state.range(0));
32-
const vector<bool> source = createRandomVector(size);
28+
const auto size = static_cast<size_t>(state.range(0));
29+
vector<bool> source = random_bool_vector(size);
3330
vector<bool> dest(size, false);
3431

3532
for (auto _ : state) {
33+
benchmark::DoNotOptimize(source);
3634
copy_n(source.cbegin() + 1, size - 1, dest.begin());
35+
benchmark::DoNotOptimize(dest);
3736
}
3837
}
3938

4039
void copy_n_dest_misaligned(benchmark::State& state) {
41-
const auto size = static_cast<size_t>(state.range(0));
42-
const vector<bool> source = createRandomVector(size);
40+
const auto size = static_cast<size_t>(state.range(0));
41+
vector<bool> source = random_bool_vector(size);
4342
vector<bool> dest(size, false);
4443

4544
for (auto _ : state) {
45+
benchmark::DoNotOptimize(source);
4646
copy_n(source.cbegin(), size - 1, dest.begin() + 1);
47+
benchmark::DoNotOptimize(dest);
4748
}
4849
}
4950

5051
// Special benchmark for matching char alignment
5152
void copy_n_matching_alignment(benchmark::State& state) {
52-
const auto size = static_cast<size_t>(state.range(0));
53-
const vector<bool> source = createRandomVector(size);
53+
const auto size = static_cast<size_t>(state.range(0));
54+
vector<bool> source = random_bool_vector(size);
5455
vector<bool> dest(size, false);
5556

5657
for (auto _ : state) {
58+
benchmark::DoNotOptimize(source);
5759
copy_n(source.cbegin() + 5, size - 5, dest.begin() + 5);
60+
benchmark::DoNotOptimize(dest);
5861
}
5962
}
6063

6164
// Special benchmarks for single block corner case
6265
void copy_n_both_single_blocks(benchmark::State& state) {
63-
const vector<bool> source = createRandomVector(50);
66+
vector<bool> source = random_bool_vector(50);
6467
vector<bool> dest(50, false);
6568

6669
const size_t length = 20;
6770
for (auto _ : state) {
71+
benchmark::DoNotOptimize(source);
6872
copy_n(source.cbegin() + 5, length, dest.begin() + 5);
73+
benchmark::DoNotOptimize(dest);
6974
}
7075
}
7176

7277
void copy_n_source_single_block(benchmark::State& state) {
73-
const vector<bool> source = createRandomVector(50);
78+
vector<bool> source = random_bool_vector(50);
7479
vector<bool> dest(50, false);
7580

7681
const size_t length = 20;
7782
for (auto _ : state) {
83+
benchmark::DoNotOptimize(source);
7884
copy_n(source.cbegin() + 5, length, dest.begin() + 25);
85+
benchmark::DoNotOptimize(dest);
7986
}
8087
}
8188

8289
void copy_n_dest_single_block(benchmark::State& state) {
83-
const vector<bool> source = createRandomVector(50);
90+
vector<bool> source = random_bool_vector(50);
8491
vector<bool> dest(50, false);
8592

8693
const size_t length = 20;
8794
for (auto _ : state) {
95+
benchmark::DoNotOptimize(source);
8896
copy_n(source.cbegin() + 25, length, dest.begin() + 5);
97+
benchmark::DoNotOptimize(dest);
8998
}
9099
}
91100

benchmarks/src/vector_bool_count.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,13 @@
88
#include <random>
99
#include <vector>
1010

11-
using namespace std;
11+
#include "utility.hpp"
1212

13-
vector<bool> createRandomVector(const size_t size) {
14-
mt19937 gen;
15-
bernoulli_distribution dist{0.5};
16-
vector<bool> result(size);
17-
generate(result.begin(), result.end(), [&] { return dist(gen); });
18-
return result;
19-
}
13+
using namespace std;
2014

2115
void count_aligned(benchmark::State& state) {
2216
const auto size = static_cast<size_t>(state.range(0));
23-
vector<bool> v = createRandomVector(size);
17+
vector<bool> v = random_bool_vector(size);
2418

2519
bool b = false;
2620

@@ -33,7 +27,6 @@ void count_aligned(benchmark::State& state) {
3327
}
3428
}
3529

36-
3730
BENCHMARK(count_aligned)->RangeMultiplier(64)->Range(64, 64 << 10);
3831

3932
BENCHMARK_MAIN();

benchmarks/src/vector_bool_move.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,93 @@
88
#include <random>
99
#include <vector>
1010

11-
using namespace std;
11+
#include "utility.hpp"
1212

13-
vector<bool> createRandomVector(const size_t size) {
14-
static mt19937 gen;
15-
vector<bool> result(size);
16-
generate_n(result.begin(), size, [] { return bernoulli_distribution{0.5}(gen); });
17-
return result;
18-
}
13+
using namespace std;
1914

2015
void move_block_aligned(benchmark::State& state) {
21-
const auto size = static_cast<size_t>(state.range(0));
22-
const vector<bool> source = createRandomVector(size);
16+
const auto size = static_cast<size_t>(state.range(0));
17+
vector<bool> source = random_bool_vector(size);
2318
vector<bool> dest(size, false);
2419

2520
for (auto _ : state) {
21+
benchmark::DoNotOptimize(source);
2622
move(source.cbegin(), source.cend(), dest.begin());
23+
benchmark::DoNotOptimize(dest);
2724
}
2825
}
2926

3027
void move_source_misaligned(benchmark::State& state) {
31-
const auto size = static_cast<size_t>(state.range(0));
32-
const vector<bool> source = createRandomVector(size);
28+
const auto size = static_cast<size_t>(state.range(0));
29+
vector<bool> source = random_bool_vector(size);
3330
vector<bool> dest(size, false);
3431

3532
for (auto _ : state) {
33+
benchmark::DoNotOptimize(source);
3634
move(source.cbegin() + 1, source.cend(), dest.begin());
35+
benchmark::DoNotOptimize(dest);
3736
}
3837
}
3938

4039
void move_dest_misaligned(benchmark::State& state) {
41-
const auto size = static_cast<size_t>(state.range(0));
42-
const vector<bool> source = createRandomVector(size);
40+
const auto size = static_cast<size_t>(state.range(0));
41+
vector<bool> source = random_bool_vector(size);
4342
vector<bool> dest(size, false);
4443

4544
for (auto _ : state) {
45+
benchmark::DoNotOptimize(source);
4646
move(source.cbegin(), source.cend() - 1, dest.begin() + 1);
47+
benchmark::DoNotOptimize(dest);
4748
}
4849
}
4950

5051
// Special benchmark for matching char alignment
5152
void move_matching_alignment(benchmark::State& state) {
52-
const auto size = static_cast<size_t>(state.range(0));
53-
const vector<bool> source = createRandomVector(size);
53+
const auto size = static_cast<size_t>(state.range(0));
54+
vector<bool> source = random_bool_vector(size);
5455
vector<bool> dest(size, false);
5556

5657
for (auto _ : state) {
58+
benchmark::DoNotOptimize(source);
5759
move(source.cbegin() + 5, source.cend(), dest.begin() + 5);
60+
benchmark::DoNotOptimize(dest);
5861
}
5962
}
6063

6164
// Special benchmarks for single block corner case
6265
void move_both_single_blocks(benchmark::State& state) {
63-
const vector<bool> source = createRandomVector(50);
66+
vector<bool> source = random_bool_vector(50);
6467
vector<bool> dest(50, false);
6568

6669
const size_t length = 20;
6770
for (auto _ : state) {
71+
benchmark::DoNotOptimize(source);
6872
move(source.cbegin() + 5, source.cbegin() + 5 + length, dest.begin() + 5);
73+
benchmark::DoNotOptimize(dest);
6974
}
7075
}
7176

7277
void move_source_single_block(benchmark::State& state) {
73-
const vector<bool> source = createRandomVector(50);
78+
vector<bool> source = random_bool_vector(50);
7479
vector<bool> dest(50, false);
7580

7681
const size_t length = 20;
7782
for (auto _ : state) {
83+
benchmark::DoNotOptimize(source);
7884
move(source.cbegin() + 5, source.cbegin() + 5 + length, dest.begin() + 25);
85+
benchmark::DoNotOptimize(dest);
7986
}
8087
}
8188

8289
void move_dest_single_block(benchmark::State& state) {
83-
const vector<bool> source = createRandomVector(50);
90+
vector<bool> source = random_bool_vector(50);
8491
vector<bool> dest(50, false);
8592

8693
const size_t length = 20;
8794
for (auto _ : state) {
95+
benchmark::DoNotOptimize(source);
8896
move(source.cbegin() + 25, source.cbegin() + 25 + length, dest.begin() + 5);
97+
benchmark::DoNotOptimize(dest);
8998
}
9099
}
91100

0 commit comments

Comments
 (0)