Skip to content

Commit 4167143

Browse files
authored
Merge pull request #13 from bugparty/codex/研究代码与标准库速度差异
Add microbenchmarks and clean bounds check config
2 parents c9bed19 + 50f9eb4 commit 4167143

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This repository provides a small bit vector implementation along with tests and
44

55
## Running the benchmarks without bounds checking
66

7-
Bounds checking is enabled by default. To benchmark without checks, configure and build with:
7+
Bounds checking is enabled by default. To benchmark without checks, configure and build with (this defines `BITVECTOR_NO_BOUND_CHECK`):
88

99
```bash
1010
cmake -S . -B build -DBITVECTOR_NO_BOUND_CHECK=ON -DCMAKE_BUILD_TYPE=Release

bitvector_benchmark.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,88 @@ static void BM_Std_Access(benchmark::State& state) {
7272
}
7373
}
7474

75+
76+
static void BM_Bowen_SetBitTrue6(benchmark::State& state) {
77+
size_t n = state.range(0);
78+
for (auto _ : state) {
79+
bitvector<> bv(n);
80+
for (size_t pos=0; pos+5 < n; pos+=6) {
81+
bv.set_bit_true_6(pos, 1);
82+
}
83+
benchmark::ClobberMemory();
84+
}
85+
}
86+
87+
static void BM_Std_SetBitTrue6(benchmark::State& state) {
88+
size_t n = state.range(0);
89+
for (auto _ : state) {
90+
std::vector<bool> bv(n);
91+
for (size_t pos=0; pos+5 < n; pos+=6) {
92+
for (int i=0;i<6;++i) {
93+
bv[pos+i] = true;
94+
}
95+
}
96+
benchmark::ClobberMemory();
97+
}
98+
}
99+
100+
static void BM_Bowen_QSetBitTrue6V2(benchmark::State& state) {
101+
size_t n = state.range(0);
102+
for (auto _ : state) {
103+
bitvector<> bv(n);
104+
bv.qset_bit_true_6_v2(0, 1, n);
105+
benchmark::ClobberMemory();
106+
}
107+
}
108+
109+
static void BM_Std_QSetBitTrue6(benchmark::State& state) {
110+
size_t n = state.range(0);
111+
for (auto _ : state) {
112+
std::vector<bool> bv(n);
113+
for (size_t i=0;i<n;++i) {
114+
bv[i] = true;
115+
}
116+
benchmark::ClobberMemory();
117+
}
118+
}
119+
120+
static void BM_Bowen_IncrementUntilZero(benchmark::State& state) {
121+
size_t n = state.range(0);
122+
bitvector<> bv(n, true);
123+
bv.set_bit(n-1, false);
124+
for (auto _ : state) {
125+
size_t pos = 0;
126+
bv.incrementUntilZero(pos);
127+
benchmark::DoNotOptimize(pos);
128+
}
129+
}
130+
131+
static void incrementUntilZeroStd(const std::vector<bool>& bv, size_t& pos) {
132+
while (pos < bv.size() && bv[pos]) ++pos;
133+
}
134+
135+
static void BM_Std_IncrementUntilZero(benchmark::State& state) {
136+
size_t n = state.range(0);
137+
std::vector<bool> bv(n, true);
138+
bv[n-1] = false;
139+
for (auto _ : state) {
140+
size_t pos = 0;
141+
incrementUntilZeroStd(bv, pos);
142+
benchmark::DoNotOptimize(pos);
143+
}
144+
}
145+
75146
BENCHMARK(BM_Bowen_Set)->Arg(1<<20)->MinTime(5.0);
76147
BENCHMARK(BM_Std_Set)->Arg(1<<20)->MinTime(5.0);
77148
BENCHMARK(BM_Bowen_PushBack)->Arg(1<<20)->MinTime(5.0);
78149
BENCHMARK(BM_Std_PushBack)->Arg(1<<20)->MinTime(5.0);
79150
BENCHMARK(BM_Bowen_Access)->Arg(1<<20)->MinTime(5.0);
80151
BENCHMARK(BM_Std_Access)->Arg(1<<20)->MinTime(5.0);
152+
BENCHMARK(BM_Bowen_SetBitTrue6)->Arg(1<<20)->MinTime(5.0);
153+
BENCHMARK(BM_Std_SetBitTrue6)->Arg(1<<20)->MinTime(5.0);
154+
BENCHMARK(BM_Bowen_QSetBitTrue6V2)->Arg(1<<20)->MinTime(5.0);
155+
BENCHMARK(BM_Std_QSetBitTrue6)->Arg(1<<20)->MinTime(5.0);
156+
BENCHMARK(BM_Bowen_IncrementUntilZero)->Arg(1<<20)->MinTime(5.0);
157+
BENCHMARK(BM_Std_IncrementUntilZero)->Arg(1<<20)->MinTime(5.0);
81158

82159
BENCHMARK_MAIN();

main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#include <chrono>
44
#include <iostream>
55
#include <vector>
6-
constexpr size_t SIZE = 1000000000; // 1 billion elements
6+
7+
// Benchmark size: 1 billion elements
8+
constexpr size_t SIZE = 1000000000;
9+
710
// Benchmarks the performance of std::vector<bool> for setting, accessing, and traversing elements.
811
void benchmarkStdVectorBool(){
912

0 commit comments

Comments
 (0)