This is an automated email from the ASF dual-hosted git repository.

liukun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new ecc62104b add bench for decimal: builder and vec to array (#2450)
ecc62104b is described below

commit ecc62104bb72fd96cb54534732f6d29362a8c573
Author: Kun Liu <[email protected]>
AuthorDate: Wed Aug 17 10:11:50 2022 +0800

    add bench for decimal: builder and vec to array (#2450)
    
    Co-authored-by: Andrew Lamb <[email protected]>
---
 arrow/benches/array_from_vec.rs | 55 ++++++++++++++++++++++++++++++++++++++++-
 arrow/benches/builder.rs        | 38 +++++++++++++++++++++++++++-
 2 files changed, 91 insertions(+), 2 deletions(-)

diff --git a/arrow/benches/array_from_vec.rs b/arrow/benches/array_from_vec.rs
index 3f82beb6f..bf318ff10 100644
--- a/arrow/benches/array_from_vec.rs
+++ b/arrow/benches/array_from_vec.rs
@@ -17,11 +17,15 @@
 
 #[macro_use]
 extern crate criterion;
+
 use criterion::Criterion;
 
 extern crate arrow;
 
 use arrow::array::*;
+use arrow::util::decimal::Decimal256;
+use num::BigInt;
+use rand::Rng;
 use std::{convert::TryFrom, sync::Arc};
 
 fn array_from_vec(n: usize) {
@@ -72,6 +76,55 @@ fn struct_array_from_vec(
     );
 }
 
+fn decimal128_array_from_vec(array: &[Option<i128>]) {
+    criterion::black_box(
+        array
+            .iter()
+            .collect::<Decimal128Array>()
+            .with_precision_and_scale(34, 2)
+            .unwrap(),
+    );
+}
+
+fn decimal256_array_from_vec() {
+    // bench decimal256array
+    // create option<into<decimal256>> array
+    let size = 1 << 10;
+    let mut array = vec![];
+    let mut rng = rand::thread_rng();
+    for _ in 0..size {
+        let decimal =
+            Decimal256::from(BigInt::from(rng.gen_range::<i128, 
_>(0..9999999999999)));
+        array.push(Some(decimal));
+    }
+    criterion::black_box(
+        array
+            .into_iter()
+            .collect::<Decimal256Array>()
+            .with_precision_and_scale(70, 2)
+            .unwrap(),
+    );
+}
+
+fn decimal_benchmark(c: &mut Criterion) {
+    // bench decimal128 array
+    // create option<i128> array
+    let size: usize = 1 << 15;
+    let mut rng = rand::thread_rng();
+    let mut array = vec![];
+    for _ in 0..size {
+        array.push(Some(rng.gen_range::<i128, _>(0..9999999999)));
+    }
+    c.bench_function("decimal128_array_from_vec 32768", |b| {
+        b.iter(|| decimal128_array_from_vec(array.as_slice()))
+    });
+
+    // bench decimal256 array
+    c.bench_function("decimal256_array_from_vec 32768", |b| {
+        b.iter(|| decimal256_array_from_vec)
+    });
+}
+
 fn criterion_benchmark(c: &mut Criterion) {
     c.bench_function("array_from_vec 128", |b| b.iter(|| array_from_vec(128)));
     c.bench_function("array_from_vec 256", |b| b.iter(|| array_from_vec(256)));
@@ -108,5 +161,5 @@ fn criterion_benchmark(c: &mut Criterion) {
     });
 }
 
-criterion_group!(benches, criterion_benchmark);
+criterion_group!(benches, criterion_benchmark, decimal_benchmark);
 criterion_main!(benches);
diff --git a/arrow/benches/builder.rs b/arrow/benches/builder.rs
index f6f90f475..691cd0683 100644
--- a/arrow/benches/builder.rs
+++ b/arrow/benches/builder.rs
@@ -22,9 +22,11 @@ extern crate rand;
 use std::mem::size_of;
 
 use criterion::*;
+use num::BigInt;
 use rand::distributions::Standard;
 
 use arrow::array::*;
+use arrow::util::decimal::Decimal256;
 use arrow::util::test_util::seedable_rng;
 use rand::Rng;
 
@@ -106,11 +108,45 @@ fn bench_string(c: &mut Criterion) {
     group.finish();
 }
 
+fn bench_decimal128(c: &mut Criterion) {
+    c.bench_function("bench_decimal128_builder", |b| {
+        b.iter(|| {
+            let mut rng = rand::thread_rng();
+            let mut decimal_builder = Decimal128Builder::new(BATCH_SIZE, 38, 
0);
+            for _ in 0..BATCH_SIZE {
+                decimal_builder
+                    .append_value(rng.gen_range::<i128, _>(0..9999999999))
+                    .unwrap();
+            }
+            black_box(decimal_builder.finish());
+        })
+    });
+}
+
+fn bench_decimal256(c: &mut Criterion) {
+    c.bench_function("bench_decimal128_builder", |b| {
+        b.iter(|| {
+            let mut rng = rand::thread_rng();
+            let mut decimal_builder = Decimal256Builder::new(BATCH_SIZE, 76, 
10);
+            for _ in 0..BATCH_SIZE {
+                decimal_builder
+                    .append_value(&Decimal256::from(BigInt::from(
+                        rng.gen_range::<i128, _>(0..99999999999),
+                    )))
+                    .unwrap()
+            }
+            black_box(decimal_builder.finish());
+        })
+    });
+}
+
 criterion_group!(
     benches,
     bench_primitive,
     bench_primitive_nulls,
     bench_bool,
-    bench_string
+    bench_string,
+    bench_decimal128,
+    bench_decimal256,
 );
 criterion_main!(benches);

Reply via email to