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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // 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
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <numeric>
6 #include <vector>
7
8 #include "base/algorithm/sorted_ranges.h"
9 #include "base/third_party/benchmark/include/benchmark/benchmark.h"
10
11 namespace {
12
13 template <class Iterator, class BinaryPredicate>
14 Iterator PreviousLastUnique(Iterator first,
15 Iterator last,
16 BinaryPredicate compare) {
17 if (first == last)
18 return last;
19
20 Iterator dest = first;
21 Iterator cur = first;
22 Iterator prev = cur;
23 while (++cur != last) {
24 if (!compare(*prev, *cur)) {
25 // Non-identical one.
26 if (dest != prev)
27 *dest = std::move(*prev);
28 ++dest;
29 }
30 prev = cur;
31 }
32
33 if (dest != prev)
34 *dest = std::move(*prev);
35 return ++dest;
36 }
37
38 template <typename Functor>
39 void LastUniqueBenchmarkManyDuplicates(benchmark::State& state, Functor impl) {
40 std::vector<int> original_input;
41
42 for (int i = 11; i < 1000000; ++i) {
43 for (int j = 0; j < i % 10; ++i)
44 original_input.push_back(i);
45 }
46
47 while (state.KeepRunning()) {
48 state.PauseTiming();
49 auto input = original_input;
50 state.ResumeTiming();
51 impl(input);
52 }
53 }
54
55 template <typename Functor>
56 void LastUniqueBenchmarkNoDuplicates(benchmark::State& state, Functor impl) {
57 std::vector<int> original_input(1000000);
58 std::iota(original_input.begin(), original_input.end(), 0);
59
60 while (state.KeepRunning()) {
61 state.PauseTiming();
62 auto input = original_input;
63 state.ResumeTiming();
64 impl(input);
65 }
66 }
67
68 BENCHMARK_CAPTURE(LastUniqueBenchmarkManyDuplicates,
69 one_cycle_and_if_many_duplicates,
70 [](std::vector<int>& in) {
71 PreviousLastUnique(in.begin(), in.end(),
72 std::equal_to<int>{});
73 });
74
75 BENCHMARK_CAPTURE(LastUniqueBenchmarkManyDuplicates,
76 two_cycles_many_duplicates,
77 [](std::vector<int>& in) {
78 base::UniquePickLast(in.begin(), in.end());
79 });
80
81 BENCHMARK_CAPTURE(LastUniqueBenchmarkNoDuplicates,
82 one_cycle_and_if_no_duplicates,
83 [](std::vector<int>& in) {
84 PreviousLastUnique(in.begin(), in.end(),
85 std::equal_to<int>{});
86 });
87
88 BENCHMARK_CAPTURE(LastUniqueBenchmarkNoDuplicates,
89 two_cycles_no_duplicates,
90 [](std::vector<int>& in) {
91 base::UniquePickLast(in.begin(), in.end());
92 });
93
94 } // namespace
95
96 BENCHMARK_MAIN()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698