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#include <algorithm>
10#include <benchmark/benchmark.h>
11#include <random>
12
13void BenchmarkSizes(benchmark::internal::Benchmark* Benchmark) {
14 Benchmark->DenseRange(start: 1, limit: 8);
15 for (size_t i = 16; i != 1 << 20; i *= 2) {
16 Benchmark->Arg(x: i - 1);
17 Benchmark->Arg(x: i);
18 Benchmark->Arg(x: i + 1);
19 }
20}
21
22// TODO: Look into benchmarking aligned and unaligned memory explicitly
23// (currently things happen to be aligned because they are malloced that way)
24template <class T>
25static void bm_mismatch(benchmark::State& state) {
26 std::vector<T> vec1(state.range(), '1');
27 std::vector<T> vec2(state.range(), '1');
28 std::mt19937_64 rng(std::random_device{}());
29
30 vec1.back() = '2';
31 for (auto _ : state) {
32 benchmark::DoNotOptimize(vec1);
33 benchmark::DoNotOptimize(std::mismatch(vec1.begin(), vec1.end(), vec2.begin()));
34 }
35}
36BENCHMARK(bm_mismatch<char>)->Apply(func: BenchmarkSizes);
37BENCHMARK(bm_mismatch<short>)->Apply(func: BenchmarkSizes);
38BENCHMARK(bm_mismatch<int>)->Apply(func: BenchmarkSizes);
39
40template <class T>
41static void bm_mismatch_two_range_overload(benchmark::State& state) {
42 std::vector<T> vec1(state.range(), '1');
43 std::vector<T> vec2(state.range(), '1');
44 std::mt19937_64 rng(std::random_device{}());
45
46 vec1.back() = '2';
47 for (auto _ : state) {
48 benchmark::DoNotOptimize(vec1);
49 benchmark::DoNotOptimize(std::mismatch(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()));
50 }
51}
52BENCHMARK(bm_mismatch_two_range_overload<char>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
53BENCHMARK(bm_mismatch_two_range_overload<short>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
54BENCHMARK(bm_mismatch_two_range_overload<int>)->DenseRange(start: 1, limit: 8)->Range(start: 16, limit: 1 << 20);
55
56BENCHMARK_MAIN();
57

source code of libcxx/benchmarks/algorithms/mismatch.bench.cpp