Skip to content

Commit 6d6ca51

Browse files
Benchmark cardinality estimators: uncorrected, HLL++, and MLE
1 parent abdb021 commit 6d6ca51

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

benches/hyperloglog_insert.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,45 @@ fn bench_insert_modes(c: &mut Criterion) {
139139
group.finish();
140140
}
141141

142-
criterion_group!(benches, bench_hyperloglog_insert, bench_insert_modes);
142+
/// Compares the cost of the cardinality estimators on a fully-fledged HyperLogLog counter: the
143+
/// default HyperLogLog++ corrected estimate, the raw uncorrected estimate, and (under the `mle`
144+
/// feature) the secant-method Maximum Likelihood Estimation.
145+
fn bench_cardinality_estimate(c: &mut Criterion) {
146+
let mut group = c.benchmark_group("cardinality_estimate_p14_bits6");
147+
148+
let hll = build_hyperloglog(200_000, 0x0000_CA4D);
149+
150+
group.bench_function("hyperloglog_pp", |b| {
151+
b.iter(|| black_box(black_box(&hll).estimate_cardinality()));
152+
});
153+
154+
group.bench_function("uncorrected", |b| {
155+
b.iter(|| black_box(black_box(&hll).uncorrected_estimate_cardinality()));
156+
});
157+
158+
#[cfg(feature = "mle")]
159+
group.bench_function("mle", |b| {
160+
b.iter(|| black_box(black_box(&hll).estimate_cardinality_mle()));
161+
});
162+
163+
group.finish();
164+
}
165+
166+
/// Builds a fully-fledged HyperLogLog counter holding `count` distinct elements.
167+
fn build_hyperloglog(count: u64, seed: u64) -> HLL14 {
168+
let mut hll = HLL14::default();
169+
for value in iter_random_values::<u64>(count, None, Some(seed)) {
170+
hll.insert(&value);
171+
}
172+
assert!(!hll.is_hash_list());
173+
hll
174+
}
175+
176+
criterion_group!(
177+
benches,
178+
bench_hyperloglog_insert,
179+
bench_insert_modes,
180+
bench_cardinality_estimate
181+
);
143182

144183
criterion_main!(benches);

0 commit comments

Comments
 (0)