#[macro_use]
extern crate criterion;
use criterion::Criterion;
use rand::distributions::{Distribution, Standard};
use rand::prelude::random;
use rand::Rng;
use std::sync::Arc;
extern crate arrow;
use arrow::array::*;
use arrow::compute::{cast, take};
use arrow::datatypes::*;
fn create_numeric<T>(size: usize) -> ArrayRef
where
T: ArrowNumericType,
Standard: Distribution<T::Native>,
PrimitiveArray<T>: std::convert::From<Vec<T::Native>>,
{
Arc::new(PrimitiveArray::<T>::from(vec![random::<T::Native>(); size])) as ArrayRef
}
fn create_random_index(size: usize) -> UInt32Array {
let mut rng = rand::thread_rng();
let ints = Int32Array::from(vec![rng.gen_range(-24i32, size as i32); size]);
UInt32Array::from(
cast(&(Arc::new(ints) as ArrayRef), &DataType::UInt32)
.unwrap()
.data(),
)
}
fn take_numeric<T>(size: usize, index_len: usize) -> ()
where
T: ArrowNumericType,
Standard: Distribution<T::Native>,
PrimitiveArray<T>: std::convert::From<Vec<T::Native>>,
T::Native: num::NumCast,
{
let array = create_numeric::<T>(size);
let index = create_random_index(index_len);
criterion::black_box(take(&array, &index, None).unwrap());
}
fn take_boolean(size: usize, index_len: usize) -> () {
let array = Arc::new(BooleanArray::from(vec![random::<bool>(); size])) as ArrayRef;
let index = create_random_index(index_len);
criterion::black_box(take(&array, &index, None).unwrap());
}
fn add_benchmark(c: &mut Criterion) {
c.bench_function("take u8 256", |b| {
b.iter(|| take_numeric::<UInt8Type>(256, 256))
});
c.bench_function("take u8 512", |b| {
b.iter(|| take_numeric::<UInt8Type>(512, 512))
});
c.bench_function("take u8 1024", |b| {
b.iter(|| take_numeric::<UInt8Type>(1024, 1024))
});
c.bench_function("take i32 256", |b| {
b.iter(|| take_numeric::<Int32Type>(256, 256))
});
c.bench_function("take i32 512", |b| {
b.iter(|| take_numeric::<Int32Type>(512, 512))
});
c.bench_function("take i32 1024", |b| {
b.iter(|| take_numeric::<Int32Type>(1024, 1024))
});
c.bench_function("take bool 256", |b| b.iter(|| take_boolean(256, 256)));
c.bench_function("take bool 512", |b| b.iter(|| take_boolean(512, 512)));
c.bench_function("take bool 1024", |b| b.iter(|| take_boolean(1024, 1024)));
}
criterion_group!(benches, add_benchmark);
criterion_main!(benches);