Skip to content

Commit 29754d7

Browse files
Improved benches for aggregates.
Added randomness to the numbers and nulls, to reduce risk of speculative stuff.
1 parent 0745764 commit 29754d7

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

rust/arrow/benches/aggregate_kernels.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
extern crate criterion;
2020
use criterion::Criterion;
2121

22+
use rand::Rng;
2223
use std::sync::Arc;
2324

2425
extern crate arrow;
@@ -27,13 +28,15 @@ use arrow::array::*;
2728
use arrow::compute::kernels::aggregate::*;
2829

2930
fn create_array(size: usize, with_nulls: bool) -> ArrayRef {
31+
// use random numbers to avoid spurious compiler optimizations wrt to branching
32+
let mut rng = rand::thread_rng();
3033
let mut builder = Float32Builder::new(size);
3134

32-
for i in 0..size {
33-
if with_nulls && i % 2 == 0 {
35+
for _ in 0..size {
36+
if with_nulls && rng.gen::<f32>() > 0.5 {
3437
builder.append_null().unwrap();
3538
} else {
36-
builder.append_value(1.0 + 1.0 * i as f32).unwrap();
39+
builder.append_value(rng.gen()).unwrap();
3740
}
3841
}
3942
Arc::new(builder.finish())
@@ -44,14 +47,21 @@ fn bench_sum(arr_a: &ArrayRef) {
4447
criterion::black_box(sum(&arr_a).unwrap());
4548
}
4649

50+
fn bench_min(arr_a: &ArrayRef) {
51+
let arr_a = arr_a.as_any().downcast_ref::<Float32Array>().unwrap();
52+
criterion::black_box(min(&arr_a).unwrap());
53+
}
54+
4755
fn add_benchmark(c: &mut Criterion) {
4856
let arr_a = create_array(512, false);
4957

5058
c.bench_function("sum 512", |b| b.iter(|| bench_sum(&arr_a)));
59+
c.bench_function("min 512", |b| b.iter(|| bench_min(&arr_a)));
5160

5261
let arr_a = create_array(512, true);
5362

5463
c.bench_function("sum nulls 512", |b| b.iter(|| bench_sum(&arr_a)));
64+
c.bench_function("min nulls 512", |b| b.iter(|| bench_min(&arr_a)));
5565
}
5666

5767
criterion_group!(benches, add_benchmark);

0 commit comments

Comments
 (0)