• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
10 
11 #include <cstdint>
12 #include <cstddef>
13 #include <functional>
14 
15 #include "benchmark/benchmark.h"
16 
17 #include "GenerateInput.h"
18 #include "test_macros.h"
19 
20 constexpr std::size_t TestNumInputs = 1024;
21 
22 template <class HashFn, class GenInputs>
BM_Hash(benchmark::State & st,HashFn fn,GenInputs gen)23 void BM_Hash(benchmark::State& st, HashFn fn, GenInputs gen) {
24   auto in               = gen(st.range(0));
25   const auto end        = in.data() + in.size();
26   std::size_t last_hash = 0;
27   benchmark::DoNotOptimize(&last_hash);
28   while (st.KeepRunning()) {
29     for (auto it = in.data(); it != end; ++it) {
30       benchmark::DoNotOptimize(last_hash += fn(*it));
31     }
32     benchmark::ClobberMemory();
33   }
34 }
35 
36 BENCHMARK_CAPTURE(BM_Hash, uint32_random_std_hash, std::hash<uint32_t>{}, getRandomIntegerInputs<uint32_t>)
37     ->Arg(TestNumInputs);
38 
39 BENCHMARK_CAPTURE(BM_Hash, uint32_top_std_hash, std::hash<uint32_t>{}, getSortedTopBitsIntegerInputs<uint32_t>)
40     ->Arg(TestNumInputs);
41 
42 BENCHMARK_MAIN();
43