From 3dd3c69d7ab60253e8e0af71b375cb1f9ac8a4ce Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Wed, 28 Oct 2020 22:25:42 +0100 Subject: [PATCH] Added bench for equality. --- rust/arrow/Cargo.toml | 4 ++ rust/arrow/benches/equal.rs | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 rust/arrow/benches/equal.rs diff --git a/rust/arrow/Cargo.toml b/rust/arrow/Cargo.toml index c7558e76a4004..7144576820771 100644 --- a/rust/arrow/Cargo.toml +++ b/rust/arrow/Cargo.toml @@ -112,3 +112,7 @@ harness = false [[bench]] name = "csv_writer" harness = false + +[[bench]] +name = "equal" +harness = false diff --git a/rust/arrow/benches/equal.rs b/rust/arrow/benches/equal.rs new file mode 100644 index 0000000000000..b70b633ff6dcf --- /dev/null +++ b/rust/arrow/benches/equal.rs @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#[macro_use] +extern crate criterion; +use criterion::Criterion; + +use rand::distributions::Alphanumeric; +use rand::Rng; +use std::sync::Arc; + +extern crate arrow; + +use arrow::array::*; + +fn create_string_array(size: usize, with_nulls: bool) -> ArrayRef { + // use random numbers to avoid spurious compiler optimizations wrt to branching + let mut rng = rand::thread_rng(); + let mut builder = StringBuilder::new(size); + + for _ in 0..size { + if with_nulls && rng.gen::() > 0.5 { + builder.append_null().unwrap(); + } else { + let string = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(10) + .collect::(); + builder.append_value(&string).unwrap(); + } + } + Arc::new(builder.finish()) +} + +fn create_array(size: usize, with_nulls: bool) -> ArrayRef { + // use random numbers to avoid spurious compiler optimizations wrt to branching + let mut rng = rand::thread_rng(); + let mut builder = Float32Builder::new(size); + + for _ in 0..size { + if with_nulls && rng.gen::() > 0.5 { + builder.append_null().unwrap(); + } else { + builder.append_value(rng.gen()).unwrap(); + } + } + Arc::new(builder.finish()) +} + +fn bench_equal(arr_a: &ArrayRef) { + let arr_a = arr_a.as_any().downcast_ref::().unwrap(); + criterion::black_box(arr_a.equals(arr_a)); +} + +fn bench_equal_string(arr_a: &ArrayRef) { + let arr_a = arr_a.as_any().downcast_ref::().unwrap(); + criterion::black_box(arr_a.equals(arr_a)); +} + +fn add_benchmark(c: &mut Criterion) { + let arr_a = create_array(512, false); + c.bench_function("equal_512", |b| b.iter(|| bench_equal(&arr_a))); + + let arr_a_nulls = create_array(512, true); + c.bench_function("equal_nulls_512", |b| b.iter(|| bench_equal(&arr_a_nulls))); + + let arr_a = create_string_array(512, false); + c.bench_function("equal_string_512", |b| { + b.iter(|| bench_equal_string(&arr_a)) + }); + + let arr_a_nulls = create_string_array(512, true); + c.bench_function("equal_string_nulls_512", |b| { + b.iter(|| bench_equal_string(&arr_a_nulls)) + }); +} + +criterion_group!(benches, add_benchmark); +criterion_main!(benches);