diff --git a/README.md b/README.md index 53a7f99..04ed213 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This repository provides a small bit vector implementation along with tests and ## Running the benchmarks without bounds checking -Bounds checking is enabled by default. To benchmark without checks, configure and build with: +Bounds checking is enabled by default. To benchmark without checks, configure and build with (this defines `BITVECTOR_NO_BOUND_CHECK`): ```bash cmake -S . -B build -DBITVECTOR_NO_BOUND_CHECK=ON -DCMAKE_BUILD_TYPE=Release diff --git a/bitvector_benchmark.cpp b/bitvector_benchmark.cpp index 4272a7e..3b593d5 100644 --- a/bitvector_benchmark.cpp +++ b/bitvector_benchmark.cpp @@ -72,11 +72,88 @@ static void BM_Std_Access(benchmark::State& state) { } } + +static void BM_Bowen_SetBitTrue6(benchmark::State& state) { + size_t n = state.range(0); + for (auto _ : state) { + bitvector<> bv(n); + for (size_t pos=0; pos+5 < n; pos+=6) { + bv.set_bit_true_6(pos, 1); + } + benchmark::ClobberMemory(); + } +} + +static void BM_Std_SetBitTrue6(benchmark::State& state) { + size_t n = state.range(0); + for (auto _ : state) { + std::vector bv(n); + for (size_t pos=0; pos+5 < n; pos+=6) { + for (int i=0;i<6;++i) { + bv[pos+i] = true; + } + } + benchmark::ClobberMemory(); + } +} + +static void BM_Bowen_QSetBitTrue6V2(benchmark::State& state) { + size_t n = state.range(0); + for (auto _ : state) { + bitvector<> bv(n); + bv.qset_bit_true_6_v2(0, 1, n); + benchmark::ClobberMemory(); + } +} + +static void BM_Std_QSetBitTrue6(benchmark::State& state) { + size_t n = state.range(0); + for (auto _ : state) { + std::vector bv(n); + for (size_t i=0;i bv(n, true); + bv.set_bit(n-1, false); + for (auto _ : state) { + size_t pos = 0; + bv.incrementUntilZero(pos); + benchmark::DoNotOptimize(pos); + } +} + +static void incrementUntilZeroStd(const std::vector& bv, size_t& pos) { + while (pos < bv.size() && bv[pos]) ++pos; +} + +static void BM_Std_IncrementUntilZero(benchmark::State& state) { + size_t n = state.range(0); + std::vector bv(n, true); + bv[n-1] = false; + for (auto _ : state) { + size_t pos = 0; + incrementUntilZeroStd(bv, pos); + benchmark::DoNotOptimize(pos); + } +} + BENCHMARK(BM_Bowen_Set)->Arg(1<<20)->MinTime(5.0); BENCHMARK(BM_Std_Set)->Arg(1<<20)->MinTime(5.0); BENCHMARK(BM_Bowen_PushBack)->Arg(1<<20)->MinTime(5.0); BENCHMARK(BM_Std_PushBack)->Arg(1<<20)->MinTime(5.0); BENCHMARK(BM_Bowen_Access)->Arg(1<<20)->MinTime(5.0); BENCHMARK(BM_Std_Access)->Arg(1<<20)->MinTime(5.0); +BENCHMARK(BM_Bowen_SetBitTrue6)->Arg(1<<20)->MinTime(5.0); +BENCHMARK(BM_Std_SetBitTrue6)->Arg(1<<20)->MinTime(5.0); +BENCHMARK(BM_Bowen_QSetBitTrue6V2)->Arg(1<<20)->MinTime(5.0); +BENCHMARK(BM_Std_QSetBitTrue6)->Arg(1<<20)->MinTime(5.0); +BENCHMARK(BM_Bowen_IncrementUntilZero)->Arg(1<<20)->MinTime(5.0); +BENCHMARK(BM_Std_IncrementUntilZero)->Arg(1<<20)->MinTime(5.0); BENCHMARK_MAIN(); diff --git a/main.cpp b/main.cpp index 6ace39c..92e2d1c 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,10 @@ #include #include #include -constexpr size_t SIZE = 1000000000; // 1 billion elements + +// Benchmark size: 1 billion elements +constexpr size_t SIZE = 1000000000; + // Benchmarks the performance of std::vector for setting, accessing, and traversing elements. void benchmarkStdVectorBool(){