1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11#include <algorithm>
12#include <benchmark/benchmark.h>
13#include <vector>
14
15// Benchmarks the worst case: check the whole range just to find out that they compare equal
16template <class T>
17static void bm_lexicographical_compare(benchmark::State& state) {
18 std::vector<T> vec1(state.range(), '1');
19 std::vector<T> vec2(state.range(), '1');
20
21 for (auto _ : state) {
22 benchmark::DoNotOptimize(vec1);
23 benchmark::DoNotOptimize(vec2);
24 benchmark::DoNotOptimize(std::lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()));
25 }
26}
27BENCHMARK(bm_lexicographical_compare<unsigned char>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
28BENCHMARK(bm_lexicographical_compare<signed char>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
29BENCHMARK(bm_lexicographical_compare<int>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
30
31template <class T>
32static void bm_ranges_lexicographical_compare(benchmark::State& state) {
33 std::vector<T> vec1(state.range(), '1');
34 std::vector<T> vec2(state.range(), '1');
35
36 for (auto _ : state) {
37 benchmark::DoNotOptimize(vec1);
38 benchmark::DoNotOptimize(vec2);
39 benchmark::DoNotOptimize(std::ranges::lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()));
40 }
41}
42BENCHMARK(bm_ranges_lexicographical_compare<unsigned char>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
43BENCHMARK(bm_ranges_lexicographical_compare<signed char>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
44BENCHMARK(bm_ranges_lexicographical_compare<int>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
45
46BENCHMARK_MAIN();
47

source code of libcxx/test/benchmarks/algorithms/lexicographical_compare.bench.cpp