Question> I try to understand the usage of insert & emplace with hint introduced to std::map. During the following test, it seems to me that the old fashion insert is fastest. Did I do something wrong here?
Thank you
static void MapEmplaceWithHint(benchmark::State& state) { std::vector<int> v{12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; std::map<int, int> mapInt; auto where(std::end(mapInt)); for (auto _ : state) { for (const auto &n : v) { // Items in non-incremental order where = mapInt.emplace_hint(where, n, n+1); } } } BENCHMARK(MapEmplaceWithHint); static void MapInsertWithHint(benchmark::State& state) { std::vector<int> v{12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; std::map<int, int> mapInt; auto where(std::end(mapInt)); for (auto _ : state) { for (const auto &n : v) { // Items in non-incremental order where = mapInt.insert(where, {n, n+1}); } } } BENCHMARK(MapInsertWithHint); static void MapInsertNoHint(benchmark::State& state) { std::vector<int> v{12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; std::map<int, int> mapInt; for (auto _ : state) { for (const auto &n : v) { // Items in non-incremental order mapInt.insert({n, n+1}); } } } BENCHMARK(MapInsertNoHint); static void MapReverseInsertNoHint(benchmark::State& state) { std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12}; std::map<int, int> mapInt; for (auto _ : state) { for (const auto &n : v) { // Items in incremental order mapInt.insert({n, n+1}); } } } BENCHMARK(MapReverseInsertNoHint); static void MapEmplaceNoHint(benchmark::State& state) { std::vector<int> v{12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; std::map<int, int> mapInt; for (auto _ : state) { for (const auto &n : v) { // Items in non-incremental order mapInt.emplace(n, n+1); } } } BENCHMARK(MapEmplaceNoHint); 



benchmark::DoNotOptimize(mapInt);after the inserting/emplacing loop in all the tests. It didn't change the outcome though :-)whereis being reassigned.benchmark::DoNotOptimizeon a variable that is the target of the whole operation so that the compiler doesn't notice that the result isn't used and throws it all away :-)