Skip to content

Commit c6f5137

Browse files
committed
[libcxx/variant] Add a few benchmarks for std::visit.
This patch adds a few `std::visit` benchmarks as a starting point. Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D85419
1 parent 30c1633 commit c6f5137

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

libcxx/benchmarks/VariantBenchmarks.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef BENCHMARK_VARIANT_BENCHMARKS_H
11+
#define BENCHMARK_VARIANT_BENCHMARKS_H
12+
13+
#include <array>
14+
#include <cstddef>
15+
#include <tuple>
16+
#include <type_traits>
17+
#include <variant>
18+
19+
#include "benchmark/benchmark.h"
20+
21+
#include "GenerateInput.h"
22+
23+
namespace VariantBenchmarks {
24+
25+
template <std::size_t I>
26+
struct S {
27+
static constexpr size_t v = I;
28+
};
29+
30+
template <std::size_t N, std::size_t... Is>
31+
static auto genVariants(std::index_sequence<Is...>) {
32+
using V = std::variant<S<Is>...>;
33+
using F = V (*)();
34+
static constexpr F fs[] = {[] { return V(std::in_place_index<Is>); }...};
35+
36+
std::array<V, N> result = {};
37+
for (auto& v : result) {
38+
v = fs[getRandomInteger(0ul, sizeof...(Is) - 1)]();
39+
}
40+
41+
return result;
42+
}
43+
44+
template <std::size_t N, std::size_t Alts>
45+
static void BM_Visit(benchmark::State& state) {
46+
auto args = genVariants<N>(std::make_index_sequence<Alts>{});
47+
for (auto _ : state) {
48+
benchmark::DoNotOptimize(std::apply(
49+
[](auto... vs) {
50+
return std::visit([](auto... is) { return (is.v + ... + 0); }, vs...);
51+
},
52+
args));
53+
}
54+
}
55+
56+
} // end namespace VariantBenchmarks
57+
58+
#endif // BENCHMARK_VARIANT_BENCHMARKS_H
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "benchmark/benchmark.h"
2+
3+
#include "VariantBenchmarks.h"
4+
5+
using namespace VariantBenchmarks;
6+
7+
BENCHMARK_TEMPLATE(BM_Visit, 1, 1);
8+
BENCHMARK_TEMPLATE(BM_Visit, 1, 2);
9+
BENCHMARK_TEMPLATE(BM_Visit, 1, 3);
10+
BENCHMARK_TEMPLATE(BM_Visit, 1, 4);
11+
BENCHMARK_TEMPLATE(BM_Visit, 1, 5);
12+
BENCHMARK_TEMPLATE(BM_Visit, 1, 6);
13+
BENCHMARK_TEMPLATE(BM_Visit, 1, 7);
14+
BENCHMARK_TEMPLATE(BM_Visit, 1, 8);
15+
BENCHMARK_TEMPLATE(BM_Visit, 1, 9);
16+
BENCHMARK_TEMPLATE(BM_Visit, 1, 10);
17+
BENCHMARK_TEMPLATE(BM_Visit, 1, 20);
18+
BENCHMARK_TEMPLATE(BM_Visit, 1, 30);
19+
BENCHMARK_TEMPLATE(BM_Visit, 1, 40);
20+
BENCHMARK_TEMPLATE(BM_Visit, 1, 50);
21+
BENCHMARK_TEMPLATE(BM_Visit, 1, 60);
22+
BENCHMARK_TEMPLATE(BM_Visit, 1, 70);
23+
BENCHMARK_TEMPLATE(BM_Visit, 1, 80);
24+
BENCHMARK_TEMPLATE(BM_Visit, 1, 90);
25+
BENCHMARK_TEMPLATE(BM_Visit, 1, 100);
26+
27+
BENCHMARK_MAIN();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "benchmark/benchmark.h"
2+
3+
#include "VariantBenchmarks.h"
4+
5+
using namespace VariantBenchmarks;
6+
7+
BENCHMARK_TEMPLATE(BM_Visit, 2, 1);
8+
BENCHMARK_TEMPLATE(BM_Visit, 2, 2);
9+
BENCHMARK_TEMPLATE(BM_Visit, 2, 3);
10+
BENCHMARK_TEMPLATE(BM_Visit, 2, 4);
11+
BENCHMARK_TEMPLATE(BM_Visit, 2, 5);
12+
BENCHMARK_TEMPLATE(BM_Visit, 2, 6);
13+
BENCHMARK_TEMPLATE(BM_Visit, 2, 7);
14+
BENCHMARK_TEMPLATE(BM_Visit, 2, 8);
15+
BENCHMARK_TEMPLATE(BM_Visit, 2, 9);
16+
BENCHMARK_TEMPLATE(BM_Visit, 2, 10);
17+
BENCHMARK_TEMPLATE(BM_Visit, 2, 20);
18+
BENCHMARK_TEMPLATE(BM_Visit, 2, 30);
19+
BENCHMARK_TEMPLATE(BM_Visit, 2, 40);
20+
BENCHMARK_TEMPLATE(BM_Visit, 2, 50);
21+
22+
BENCHMARK_MAIN();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "benchmark/benchmark.h"
2+
3+
#include "VariantBenchmarks.h"
4+
5+
using namespace VariantBenchmarks;
6+
7+
BENCHMARK_TEMPLATE(BM_Visit, 3, 1);
8+
BENCHMARK_TEMPLATE(BM_Visit, 3, 2);
9+
BENCHMARK_TEMPLATE(BM_Visit, 3, 3);
10+
BENCHMARK_TEMPLATE(BM_Visit, 3, 4);
11+
BENCHMARK_TEMPLATE(BM_Visit, 3, 5);
12+
BENCHMARK_TEMPLATE(BM_Visit, 3, 6);
13+
BENCHMARK_TEMPLATE(BM_Visit, 3, 7);
14+
BENCHMARK_TEMPLATE(BM_Visit, 3, 8);
15+
BENCHMARK_TEMPLATE(BM_Visit, 3, 9);
16+
BENCHMARK_TEMPLATE(BM_Visit, 3, 10);
17+
BENCHMARK_TEMPLATE(BM_Visit, 3, 15);
18+
BENCHMARK_TEMPLATE(BM_Visit, 3, 20);
19+
20+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)