43 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 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 20constexpr std::size_t TestNumInputs = 1024;21 22template <class HashFn, class GenInputs>23void 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 36BENCHMARK_CAPTURE(BM_Hash, uint32_random_std_hash, std::hash<uint32_t>{}, getRandomIntegerInputs<uint32_t>)37 ->Arg(TestNumInputs);38 39BENCHMARK_CAPTURE(BM_Hash, uint32_top_std_hash, std::hash<uint32_t>{}, getSortedTopBitsIntegerInputs<uint32_t>)40 ->Arg(TestNumInputs);41 42BENCHMARK_MAIN();43