Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Unified Diff: base/algorithm/benchmarks.cc

Issue 2807933002: Removing (dest != prev) check from LastUnique algorithm. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/algorithm/benchmarks.cc
diff --git a/base/algorithm/benchmarks.cc b/base/algorithm/benchmarks.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b719d1e1fcca09a6e2eb2f3e0ff2e48255976230
--- /dev/null
+++ b/base/algorithm/benchmarks.cc
@@ -0,0 +1,96 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
dyaroshev 2017/04/08 16:48:03 I will remove these benchmarks before merging. I'v
dyaroshev 2017/04/11 10:07:10 On 2017/04/08 16:48:03, dyaroshev wrote:
brettw 2017/04/26 21:48:51 You can add them to the //base:base_perftests targ
dyaroshev 2017/04/27 19:11:46 Unfortunately, it would break your builds. Google
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <numeric>
+#include <vector>
+
+#include "base/algorithm/sorted_ranges.h"
+#include "base/third_party/benchmark/include/benchmark/benchmark.h"
+
+namespace {
+
+template <class Iterator, class BinaryPredicate>
+Iterator PreviousLastUnique(Iterator first,
+ Iterator last,
+ BinaryPredicate compare) {
+ if (first == last)
+ return last;
+
+ Iterator dest = first;
+ Iterator cur = first;
+ Iterator prev = cur;
+ while (++cur != last) {
+ if (!compare(*prev, *cur)) {
+ // Non-identical one.
+ if (dest != prev)
+ *dest = std::move(*prev);
+ ++dest;
+ }
+ prev = cur;
+ }
+
+ if (dest != prev)
+ *dest = std::move(*prev);
+ return ++dest;
+}
+
+template <typename Functor>
+void LastUniqueBenchmarkManyDuplicates(benchmark::State& state, Functor impl) {
+ std::vector<int> original_input;
+
+ for (int i = 11; i < 1000000; ++i) {
+ for (int j = 0; j < i % 10; ++i)
+ original_input.push_back(i);
+ }
+
+ while (state.KeepRunning()) {
+ state.PauseTiming();
+ auto input = original_input;
+ state.ResumeTiming();
+ impl(input);
+ }
+}
+
+template <typename Functor>
+void LastUniqueBenchmarkNoDuplicates(benchmark::State& state, Functor impl) {
+ std::vector<int> original_input(1000000);
+ std::iota(original_input.begin(), original_input.end(), 0);
+
+ while (state.KeepRunning()) {
+ state.PauseTiming();
+ auto input = original_input;
+ state.ResumeTiming();
+ impl(input);
+ }
+}
+
+BENCHMARK_CAPTURE(LastUniqueBenchmarkManyDuplicates,
+ one_cycle_and_if_many_duplicates,
+ [](std::vector<int>& in) {
+ PreviousLastUnique(in.begin(), in.end(),
+ std::equal_to<int>{});
+ });
+
+BENCHMARK_CAPTURE(LastUniqueBenchmarkManyDuplicates,
+ two_cycles_many_duplicates,
+ [](std::vector<int>& in) {
+ base::UniquePickLast(in.begin(), in.end());
+ });
+
+BENCHMARK_CAPTURE(LastUniqueBenchmarkNoDuplicates,
+ one_cycle_and_if_no_duplicates,
+ [](std::vector<int>& in) {
+ PreviousLastUnique(in.begin(), in.end(),
+ std::equal_to<int>{});
+ });
+
+BENCHMARK_CAPTURE(LastUniqueBenchmarkNoDuplicates,
+ two_cycles_no_duplicates,
+ [](std::vector<int>& in) {
+ base::UniquePickLast(in.begin(), in.end());
+ });
+
+} // namespace
+
+BENCHMARK_MAIN()

Powered by Google App Engine
This is Rietveld 408576698