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