53 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 <unordered_map>12#include <utility>13 14#include "associative_container_benchmarks.h"15#include "../../GenerateInput.h"16#include "benchmark/benchmark.h"17 18static void BM_map_find_string_literal(benchmark::State& state) {19 std::unordered_map<std::string, int> map;20 map.emplace("Something very very long to show a long string situation", 1);21 map.emplace("Something Else", 2);22 23 for (auto _ : state) {24 benchmark::DoNotOptimize(map);25 benchmark::DoNotOptimize(map.find("Something very very long to show a long string situation"));26 }27}28 29BENCHMARK(BM_map_find_string_literal);30 31template <class K, class V>32struct support::adapt_operations<std::unordered_map<K, V>> {33 using ValueType = typename std::unordered_map<K, V>::value_type;34 using KeyType = typename std::unordered_map<K, V>::key_type;35 static ValueType value_from_key(KeyType const& k) { return {k, Generate<V>::arbitrary()}; }36 static KeyType key_from_value(ValueType const& value) { return value.first; }37 38 using InsertionResult = std::pair<typename std::unordered_map<K, V>::iterator, bool>;39 static auto get_iterator(InsertionResult const& result) { return result.first; }40 41 template <class Allocator>42 using rebind_alloc = std::unordered_map<K, V, std::hash<K>, std::equal_to<K>, Allocator>;43};44 45int main(int argc, char** argv) {46 support::associative_container_benchmarks<std::unordered_map<int, int>>("std::unordered_map<int, int>");47 48 benchmark::Initialize(&argc, argv);49 benchmark::RunSpecifiedBenchmarks();50 benchmark::Shutdown();51 return 0;52}53