Skip to content

Commit

Permalink
Added hashsets benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaCappelletti94 committed Dec 5, 2023
1 parent 5eafc7e commit a4d03f0
Showing 1 changed file with 112 additions and 8 deletions.
120 changes: 112 additions & 8 deletions benches/estimated_overlap_and_differences_cardinality_matrices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@
extern crate test;

use hyperloglog_rs::prelude::*;
use std::collections::HashSet;
use core::mem::MaybeUninit;

use test::{black_box, Bencher};

// Method to allocate an array of HashSets using maybe uninitialized memory,
// so to circumvent the fact that HashSet does not implement Copy.
fn allocate_array_of_hashsets<const N: usize, T>() -> [HashSet<T>; N] {
unsafe {
let mut array: [HashSet<T>; N] = MaybeUninit::uninit().assume_init();
for i in 0..N {
// We replace the previosly initialized value with an hashset
// and we forget the previous value.
std::mem::forget(std::mem::replace(&mut array[i], HashSet::new()));
}
array
}
}

fn populate_vectors<const N: usize>() -> (
[HyperLogLog<Precision4, 4>; N],
[HyperLogLog<Precision4, 4>; N],
[HyperLogLog<Precision12, 6>; N],
[HyperLogLog<Precision12, 6>; N],
) {
// Optionally include some setup
const NUMBER_OF_ELEMENTS: usize = 100;
Expand All @@ -17,8 +33,8 @@ fn populate_vectors<const N: usize>() -> (
// estimated overlap cardinality matrices:

// Create the counters
let mut left: [HyperLogLog<Precision4, 4>; N] = [HyperLogLog::default(); N];
let mut right: [HyperLogLog<Precision4, 4>; N] = [HyperLogLog::default(); N];
let mut left: [HyperLogLog<Precision12, 6>; N] = [HyperLogLog::default(); N];
let mut right: [HyperLogLog<Precision12, 6>; N] = [HyperLogLog::default(); N];

left[0].insert(&56);
right[0].insert(&32);
Expand All @@ -41,13 +57,49 @@ fn populate_vectors<const N: usize>() -> (
(left, right)
}

fn populate_hashsets<const N: usize>() -> (
[HashSet<usize>; N],
[HashSet<usize>; N],
) {
// Optionally include some setup
const NUMBER_OF_ELEMENTS: usize = 100;

// We create the counters, populate them with data and
// then we create the arrays to use to estimate the
// estimated overlap cardinality matrices:

// Create the counters
let mut left: [HashSet<usize>; N] = allocate_array_of_hashsets();
let mut right: [HashSet<usize>; N] = allocate_array_of_hashsets();

left[0].insert(56);
right[0].insert(32);

// Populate the counters
for i in 1..N {
// We make sure that all values in the leftmost
// counters are contained in the rightmost counters
let tmp = left[i].union(&left[i - 1]).copied().collect();
left[i] = tmp;
let tmp = right[i].union(&right[i - 1]).copied().collect();
right[i] = tmp;
for j in 1..NUMBER_OF_ELEMENTS {
// We populate the countes
left[i].insert((j * i * 3) % 20);
right[i].insert((j * i * 7) % 20);
}
}

(left, right)
}

#[bench]
fn bench_overlap_and_differences_cardinality_matrices_2(b: &mut Bencher) {
let (left, right) = populate_vectors::<2>();

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..10_000 {
black_box(for _ in 0..100 {
let _: ([[f32; 2]; 2], [f32; 2], [f32; 2]) =
HyperLogLog::overlap_and_differences_cardinality_matrices::<2, 2>(&left, &right);
});
Expand All @@ -60,7 +112,7 @@ fn bench_overlap_and_differences_cardinality_matrices_3(b: &mut Bencher) {

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..10_000 {
black_box(for _ in 0..100 {
let _: ([[f32; 3]; 3], [f32; 3], [f32; 3]) =
HyperLogLog::overlap_and_differences_cardinality_matrices::<3, 3>(&left, &right);
});
Expand All @@ -73,7 +125,7 @@ fn bench_overlap_and_differences_cardinality_matrices_4(b: &mut Bencher) {

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..10_000 {
black_box(for _ in 0..100 {
let _: ([[f32; 4]; 4], [f32; 4], [f32; 4]) =
HyperLogLog::overlap_and_differences_cardinality_matrices::<4, 4>(&left, &right);
});
Expand All @@ -86,9 +138,61 @@ fn bench_overlap_and_differences_cardinality_matrices_5(b: &mut Bencher) {

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..10_000 {
black_box(for _ in 0..100 {
let _: ([[f32; 5]; 5], [f32; 5], [f32; 5]) =
HyperLogLog::overlap_and_differences_cardinality_matrices::<5, 5>(&left, &right);
});
});
}

#[bench]
fn bench_overlap_and_differences_cardinality_matrices_2_exact(b: &mut Bencher) {
let (left, right) = populate_hashsets::<2>();

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..100 {
let _: ([[f32; 2]; 2], [f32; 2], [f32; 2]) =
HashSet::overlap_and_differences_cardinality_matrices::<2, 2>(&left, &right);
});
});
}

#[bench]
fn bench_overlap_and_differences_cardinality_matrices_3_exact(b: &mut Bencher) {
let (left, right) = populate_hashsets::<3>();

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..100 {
let _: ([[f32; 3]; 3], [f32; 3], [f32; 3]) =
HashSet::overlap_and_differences_cardinality_matrices::<3, 3>(&left, &right);
});
});
}

#[bench]
fn bench_overlap_and_differences_cardinality_matrices_4_exact(b: &mut Bencher) {
let (left, right) = populate_hashsets::<4>();

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..100 {
let _: ([[f32; 4]; 4], [f32; 4], [f32; 4]) =
HashSet::overlap_and_differences_cardinality_matrices::<4, 4>(&left, &right);
});
});
}

#[bench]
fn bench_overlap_and_differences_cardinality_matrices_5_exact(b: &mut Bencher) {
let (left, right) = populate_hashsets::<5>();

b.iter(|| {
// Inner closure, the actual test
black_box(for _ in 0..100 {
let _: ([[f32; 5]; 5], [f32; 5], [f32; 5]) =
HashSet::overlap_and_differences_cardinality_matrices::<5, 5>(&left, &right);
});
});
}

0 comments on commit a4d03f0

Please sign in to comment.