1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 #include "benchmark/benchmark.h"
15 template <std::size_t Indx
, std::size_t Depth
>
16 struct C
: public virtual C
<Indx
, Depth
- 1>, public virtual C
<Indx
+ 1, Depth
- 1> {
20 template <std::size_t Indx
>
25 template <std::size_t Indx
, std::size_t Depth
>
26 struct B
: public virtual C
<Indx
, Depth
- 1>, public virtual C
<Indx
+ 1, Depth
- 1> {};
28 template <class Indx
, std::size_t Depth
>
31 template <std::size_t... Indx
, std::size_t Depth
>
32 struct makeB
<std::index_sequence
<Indx
...>, Depth
> : public B
<Indx
, Depth
>... {};
34 template <std::size_t Width
, std::size_t Depth
>
35 struct A
: public makeB
<std::make_index_sequence
<Width
>, Depth
> {};
37 constexpr std::size_t Width
= 10;
38 constexpr std::size_t Depth
= 5;
40 template <typename Destination
>
41 void CastTo(benchmark::State
& state
) {
43 auto base
= static_cast<C
<Width
/ 2, 0>*>(&a
);
45 Destination
* b
= nullptr;
46 for (auto _
: state
) {
47 b
= dynamic_cast<Destination
*>(base
);
48 benchmark::DoNotOptimize(b
);
54 BENCHMARK(CastTo
<B
<Width
/ 2, Depth
>>);
55 BENCHMARK(CastTo
<A
<Width
, Depth
>>);
60 * Benchmark results: (release builds)
63 * ----------------------------------------------------------------------
64 * Benchmark Time CPU Iterations
65 * ----------------------------------------------------------------------
66 * CastTo<B<Width / 2, Depth>> 1997 ns 1997 ns 349247
67 * CastTo<A<Width, Depth>> 256 ns 256 ns 2733871
70 * ----------------------------------------------------------------------
71 * Benchmark Time CPU Iterations
72 * ----------------------------------------------------------------------
73 * CastTo<B<Width / 2, Depth>> 5240 ns 5240 ns 133091
74 * CastTo<A<Width, Depth>> 866 ns 866 ns 808600