Skip to content

Commit

Permalink
update pushback_benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
wusatosi committed Jan 31, 2025
1 parent 6a3337e commit 782fec7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
6 changes: 6 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ target_link_libraries(
beman.inplace_vector.benchmark.push_back
PRIVATE beman.inplace_vector benchmark::benchmark
)

add_executable(beman.inplace_vector.benchmark.access access_benchmark.cpp)
target_link_libraries(
beman.inplace_vector.benchmark.access
PRIVATE beman.inplace_vector benchmark::benchmark
)
64 changes: 46 additions & 18 deletions benchmarks/pushback_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,70 @@
#include "beman/inplace_vector/inplace_vector.hpp"

#include <benchmark/benchmark.h>
#include <cstddef>
#include <iostream>
#include <vector>

using beman::inplace_vector;

template <std::size_t Capacity>
void InplaceVectorPushback(benchmark::State &state) {
for (auto _ : state) {
using IV = inplace_vector<int, Capacity>;
template <std::size_t Capacity> void InplaceVector(benchmark::State &state) {
using IV = inplace_vector<int, Capacity>;
IV v;
v.reserve(Capacity);

IV v;
while (state.KeepRunningBatch(Capacity)) {
// while (state.KeepRunning()) {
v.clear();
for (typename IV::size_type i = 0; i < Capacity; ++i)
v.push_back(i);
benchmark::DoNotOptimize(v);
}
}

template <std::size_t Capacity> void VetorPushback(benchmark::State &state) {
for (auto _ : state) {
using IV = std::vector<int>;
template <std::size_t Capacity> void StdVector(benchmark::State &state) {
using IV = std::vector<int>;
IV v;
v.reserve(Capacity);

IV v;
v.reserve(Capacity);
while (state.KeepRunningBatch(Capacity)) {
// while (state.KeepRunning()) {
v.clear();
for (typename IV::size_type i = 0; i < Capacity; ++i)
v.push_back(i);
benchmark::DoNotOptimize(v);
}
}

BENCHMARK_TEMPLATE(InplaceVectorPushback, 128);
BENCHMARK_TEMPLATE(InplaceVectorPushback, 256);
BENCHMARK_TEMPLATE(InplaceVectorPushback, 512);
BENCHMARK_TEMPLATE(InplaceVectorPushback, 1024);
template <typename V, bool use_reserve> struct Meta {
using Vector = V;
constexpr static bool Reserve = use_reserve;
};

#define BENCHMARK_VEC_L1(ARG) \
BENCHMARK_TEMPLATE(ARG, 3); \
BENCHMARK_TEMPLATE(ARG, 7); \
BENCHMARK_TEMPLATE(ARG, 15); \
BENCHMARK_TEMPLATE(ARG, 31); \
BENCHMARK_TEMPLATE(ARG, 63); \
BENCHMARK_TEMPLATE(ARG, 127); \
BENCHMARK_TEMPLATE(ARG, 255); \
BENCHMARK_TEMPLATE(ARG, 511); \
BENCHMARK_TEMPLATE(ARG, 1023);

#define BENCHMARK_VEC_L2_L3(ARG) \
BENCHMARK_TEMPLATE(ARG, 1 << 10); \
BENCHMARK_TEMPLATE(ARG, 1 << 11); \
BENCHMARK_TEMPLATE(ARG, 1 << 12); \
BENCHMARK_TEMPLATE(ARG, 1 << 14); \
BENCHMARK_TEMPLATE(ARG, 1 << 15); \
BENCHMARK_TEMPLATE(ARG, 1 << 16); \
BENCHMARK_TEMPLATE(ARG, 1 << 17); \
BENCHMARK_TEMPLATE(ARG, 1 << 18);

BENCHMARK_VEC_L1(InplaceVector);
BENCHMARK_VEC_L1(StdVector);

BENCHMARK_TEMPLATE(VetorPushback, 128);
BENCHMARK_TEMPLATE(VetorPushback, 256);
BENCHMARK_TEMPLATE(VetorPushback, 512);
BENCHMARK_TEMPLATE(VetorPushback, 1024);
BENCHMARK_VEC_L2_L3(InplaceVector);
BENCHMARK_VEC_L2_L3(StdVector);

BENCHMARK_MAIN();

0 comments on commit 782fec7

Please sign in to comment.