Skip to content

Commit

Permalink
swap_ranges benchmark (#4589)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
  • Loading branch information
AlexGuteniev and StephanTLavavej authored Apr 19, 2024
1 parent 1b06c52 commit 23156f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ add_benchmark(priority_queue_push_range src/priority_queue_push_range.cpp)
add_benchmark(random_integer_generation src/random_integer_generation.cpp)
add_benchmark(replace src/replace.cpp)
add_benchmark(std_copy src/std_copy.cpp)
add_benchmark(swap_ranges src/swap_ranges.cpp)

add_benchmark(vector_bool_copy src/std/containers/sequences/vector.bool/copy/test.cpp)
add_benchmark(vector_bool_copy_n src/std/containers/sequences/vector.bool/copy_n/test.cpp)
Expand Down
26 changes: 26 additions & 0 deletions benchmarks/src/swap_ranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>
#include <benchmark/benchmark.h>
#include <cstddef>
#include <cstdint>
#include <vector>

using namespace std;

template <class T>
void bm(benchmark::State& state) {
vector<T> a(static_cast<size_t>(state.range(0)), T{'a'});
vector<T> b(static_cast<size_t>(state.range(0)), T{'b'});

for (auto _ : state) {
swap_ranges(a.begin(), a.end(), b.begin());
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
}
}

BENCHMARK(bm<uint8_t>)->Arg(1)->Arg(5)->Arg(15)->Arg(26)->Arg(38)->Arg(60)->Arg(125)->Arg(800)->Arg(3000)->Arg(9000);

BENCHMARK_MAIN();

0 comments on commit 23156f1

Please sign in to comment.