File: lexicographical_compare_three_way.bench.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (89 lines) | stat: -rw-r--r-- 3,318 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

#include <algorithm>

#include "benchmark/benchmark.h"
#include "test_iterators.h"

static void BM_lexicographical_compare_three_way_slow_path(benchmark::State& state) {
  auto size = state.range(0);
  std::vector<int> v1;
  v1.resize(size);
  // v2 is identical except for the last value.
  // This means, that `lexicographical_compare_three_way` actually has to
  // compare the complete vector and cannot bail out early.
  std::vector<int> v2 = v1;
  v2.back() += 1;
  int* b1 = v1.data();
  int* e1 = b1 + v1.size();
  int* b2 = v2.data();
  int* e2 = b2 + v2.size();

  for (auto _ : state) {
    auto cmp = std::compare_three_way();
    benchmark::DoNotOptimize(std::__lexicographical_compare_three_way_slow_path(b1, e1, b2, e2, cmp));
  }
}

BENCHMARK(BM_lexicographical_compare_three_way_slow_path)->RangeMultiplier(4)->Range(1, 1 << 20);

static void BM_lexicographical_compare_three_way_fast_path(benchmark::State& state) {
  auto size = state.range(0);
  std::vector<int> v1;
  v1.resize(size);
  // v2 is identical except for the last value.
  // This means, that `lexicographical_compare_three_way` actually has to
  // compare the complete vector and cannot bail out early.
  std::vector<int> v2 = v1;
  v2.back() += 1;
  int* b1 = v1.data();
  int* e1 = b1 + v1.size();
  int* b2 = v2.data();
  int* e2 = b2 + v2.size();

  for (auto _ : state) {
    auto cmp = std::compare_three_way();
    benchmark::DoNotOptimize(std::__lexicographical_compare_three_way_fast_path(b1, e1, b2, e2, cmp));
  }
}

BENCHMARK(BM_lexicographical_compare_three_way_fast_path)->RangeMultiplier(4)->Range(1, 1 << 20);

template <class IteratorT>
static void BM_lexicographical_compare_three_way(benchmark::State& state) {
  auto size = state.range(0);
  std::vector<int> v1;
  v1.resize(size);
  // v2 is identical except for the last value.
  // This means, that `lexicographical_compare_three_way` actually has to
  // compare the complete vector and cannot bail out early.
  std::vector<int> v2 = v1;
  v2.back() += 1;
  auto b1 = IteratorT{v1.data()};
  auto e1 = IteratorT{v1.data() + v1.size()};
  auto b2 = IteratorT{v2.data()};
  auto e2 = IteratorT{v2.data() + v2.size()};

  for (auto _ : state) {
    benchmark::DoNotOptimize(std::lexicographical_compare_three_way(b1, e1, b2, e2));
  }
}

// Type alias to make sure the `*` does not appear in the benchmark name.
// A `*` would confuse the Python test runner running this google benchmark.
using IntPtr = int*;

// `lexicographical_compare_three_way` has a fast path for random access iterators.
BENCHMARK(BM_lexicographical_compare_three_way<IntPtr>)->RangeMultiplier(4)->Range(1, 1 << 20);
BENCHMARK(BM_lexicographical_compare_three_way<random_access_iterator<IntPtr>>)->RangeMultiplier(4)->Range(1, 1 << 20);
BENCHMARK(BM_lexicographical_compare_three_way<cpp17_input_iterator<IntPtr>>)->RangeMultiplier(4)->Range(1, 1 << 20);

BENCHMARK_MAIN();