Index: base/containers/containers_benchmarks.cc |
diff --git a/base/containers/containers_benchmarks.cc b/base/containers/containers_benchmarks.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fa22ad6917efcfeccec62e8c531597cfaaf46b81 |
--- /dev/null |
+++ b/base/containers/containers_benchmarks.cc |
@@ -0,0 +1,54 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <algorithm> |
+#include <array> |
+#include <iterator> |
+#include <numeric> |
+#include <set> |
+ |
+#include "base/containers/flat_set.h" |
+#include "third_party/google_benchmark/include/benchmark/benchmark.h" |
+ |
+namespace { |
+ |
+template <typename> |
+struct TypeTag {}; |
+ |
+base::flat_set<int> GenerateSet(const std::vector<int>& input, |
+ TypeTag<base::flat_set<int>>) { |
+ return {input.begin(), input.end(), base::KEEP_FIRST_OF_DUPES}; |
+} |
+ |
+std::set<int> GenerateSet(const std::vector<int>& input, |
+ TypeTag<std::set<int>>) { |
+ return {input.begin(), input.end()}; |
+} |
+ |
+template <typename Set> |
+void InsertInTheBeginingByOne(benchmark::State& state) { |
+ constexpr int kNewElementsCount = 10; |
+ std::array<int, kNewElementsCount> new_elements; |
+ std::iota(new_elements.begin(), new_elements.end(), 0); |
+ std::vector<int> already_in(state.range(0)); |
+ std::iota(already_in.begin(), already_in.end(), kNewElementsCount); |
+ |
+ while (state.KeepRunning()) { |
+ state.PauseTiming(); |
+ Set test_set = GenerateSet(already_in, TypeTag<Set>{}); |
+ state.ResumeTiming(); |
+ std::copy(new_elements.begin(), new_elements.end(), |
+ std::inserter(test_set, test_set.begin())); |
+ } |
+} |
+ |
+constexpr int kMinSize = 0; |
+constexpr int kMaxSize = 1 << 10; |
+ |
+BENCHMARK_TEMPLATE(InsertInTheBeginingByOne, std::set<int>) |
+ ->Range(kMinSize, kMaxSize); |
+BENCHMARK_TEMPLATE(InsertInTheBeginingByOne, base::flat_set<int>) |
+ ->Range(kMinSize, kMaxSize); |
+ |
+} // namespace |