-
Notifications
You must be signed in to change notification settings - Fork 0
/
fenwick_rurq_benchmark.cc
173 lines (160 loc) · 5.46 KB
/
fenwick_rurq_benchmark.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2018 Viktor Slavkovic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <random>
#include <unordered_map>
#include <vector>
#include "absl/strings/substitute.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "fenwick_rurq.h"
using fenwick::FenwickRURQ;
////////////////////////////////////////////////////////////////////////////////
// MEASUREMENT METHODS
////////////////////////////////////////////////////////////////////////////////
absl::Duration measure_prefix_sum(FenwickRURQ* ft, int ntc,
std::function<int64_t()> idx_gen) {
// Generate ntc indexes to query.
std::vector<int> idxs;
for (int i = 0; i < ntc; i++) {
idxs.push_back(idx_gen());
}
// Do the queries.
auto start = absl::Now();
for (int tc = 0; tc < ntc; tc++) {
ft->prefix_sum(idxs[tc]);
}
auto duration = absl::Now() - start;
return duration / ntc;
}
absl::Duration measure_range_sum(FenwickRURQ* ft, int ntc,
std::function<int()> idx_gen) {
// Generate.
std::vector<int> ls;
std::vector<int> rs;
for (int i = 0; i < ntc; i++) {
int l = idx_gen();
int r = idx_gen();
if (l > r) {
std::swap(l, r);
}
ls.push_back(l);
rs.push_back(r);
}
// Do the queries.
auto start = absl::Now();
for (int tc = 0; tc < ntc; tc++) {
ft->range_sum(ls[tc], rs[tc]);
}
auto duration = absl::Now() - start;
return duration / ntc;
}
absl::Duration measure_update(FenwickRURQ* ft, int ntc,
std::function<int()> idx_gen,
std::function<int()> val_gen) {
// Generate.
std::vector<int> ls;
std::vector<int> rs;
std::vector<int64_t> vals;
for (int i = 0; i < ntc; i++) {
int l = idx_gen();
int r = idx_gen();
if (l > r) {
std::swap(l, r);
}
ls.push_back(l);
rs.push_back(r);
vals.push_back(val_gen());
}
// Do the updates.
auto start = absl::Now();
for (int tc = 0; tc < ntc; tc++) {
ft->update(ls[tc], rs[tc], vals[tc]);
}
auto duration = absl::Now() - start;
return duration / ntc;
}
////////////////////////////////////////////////////////////////////////////////
constexpr int64_t kMaxVal = 1000;
constexpr int kMinOrder = 7;
constexpr int kMaxOrder = 28;
const std::unordered_map<int, int> kNumEachPerOrder {
{ 7, 10000}, { 8, 10000}, { 9, 10000}, {10, 10000}, {11, 10000}, {12, 1000},
{13, 1000}, {14, 1000}, {15, 1000}, {16, 1000}, {17, 800}, {18, 500},
{19, 100}, {20, 100}, {21, 100}, {22, 100}, {23, 100}, {24, 100},
{25, 50}, {26, 50}, {27, 50}, {28, 50}, {29, 50}, {30, 50},
};
// #define PLOT_DUMP
int main() {
std::random_device rd;
std::mt19937 prng(rd());
std::uniform_int_distribution<int64_t> elem_dist(0, kMaxVal);
auto val_gen = [&]() { return elem_dist(prng); };
for (int order = kMinOrder; order <= kMaxOrder; order++) {
#ifndef PLOT_DUMP
printf("Order: %d\n", order);
#else
printf("%d\t", order);
#endif
FenwickRURQ ft(order);
int n = (1 << order) - 1;
std::uniform_int_distribution<int> idx_dist(1, n);
auto idx_gen = [&]() { return idx_dist(prng); };
const int kNumEach = kNumEachPerOrder.at(order);
// 1) Prefix Sum
{
auto duration = measure_prefix_sum(&ft, kNumEach, idx_gen);
#ifndef PLOT_DUMP
auto msg = absl::Substitute("$0 x prefix_sum: $1: ", kNumEach,
absl::FormatDuration(duration));
printf(" %s\n", msg.c_str());
#else
printf("%15.3lf\t", ToDoubleNanoseconds(duration));
#endif
}
// 2) Update
{
auto duration = measure_update(&ft, kNumEach, idx_gen, val_gen);
#ifndef PLOT_DUMP
auto msg = absl::Substitute("$0 x update: $1: ", kNumEach,
absl::FormatDuration(duration));
printf(" %s\n", msg.c_str());
#else
printf("%15.3lf\t", ToDoubleNanoseconds(duration));
#endif
}
// 3) Range Sum
{
auto duration = measure_range_sum(&ft, kNumEach, idx_gen);
#ifndef PLOT_DUMP
auto msg = absl::Substitute("$0 x range_sum: $1: ", kNumEach,
absl::FormatDuration(duration));
printf(" %s\n", msg.c_str());
#else
printf("%15.3lf\t", ToDoubleNanoseconds(duration));
#endif
}
printf("\n");
}
return 0;
}