Skip to content

Instantly share code, notes, and snippets.

@sundy-li
Created December 27, 2021 03:50
Show Gist options
  • Save sundy-li/346258710ce1af1f8391f7718cd6321c to your computer and use it in GitHub Desktop.
Save sundy-li/346258710ce1af1f8391f7718cd6321c to your computer and use it in GitHub Desktop.
bench_arrow
// 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 arrow2::buffer::Buffer;
use criterion::Criterion;
use arrow2::array::*;
use arrow2::datatypes::*;
use arrow2::util::bench_util::*;
use num_traits::AsPrimitive;
fn add_benchmark(c: &mut Criterion) {
let size = 1048576;
let i32_array = create_primitive_array::<i32>(size, 0.0);
c.bench_function("abs_d", |b| {
b.iter(|| criterion::black_box(abs_d(&i32_array)))
});
c.bench_function("abs_v", |b| {
b.iter(|| criterion::black_box(abs_v(&i32_array)))
});
c.bench_function("abs_e", |b| {
b.iter(|| criterion::black_box(abs_e(&i32_array)))
});
}
fn abs_d(array: &PrimitiveArray<i32>) -> PrimitiveArray<u32> {
let it = array.values().iter().map(|x| x.abs() as u32);
unsafe { PrimitiveArray::<u32>::from_trusted_len_values_iter_unchecked(it) }
}
fn abs_v(array: &PrimitiveArray<i32>) -> PrimitiveArray<u32> {
let mut data = vec![0u32; array.len()];
array.values().iter().enumerate().for_each(|(i, x)| {
data[i] = x.abs() as u32;
});
PrimitiveArray::<u32>::from_vec(data)
}
fn abs_e(array: &PrimitiveArray<i32>) -> PrimitiveArray<u32> {
let mut data = Vec::with_capacity(array.len());
unsafe { data.set_len(array.len()) }
array.values().iter().enumerate().for_each(|(i, x)| {
data[i] = x.abs() as u32;
});
PrimitiveArray::<u32>::from_vec(data)
}
criterion_group!(benches, add_benchmark);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment