RTEnzyme commented on issue #8816:
URL: 
https://github.com/apache/arrow-datafusion/issues/8816#issuecomment-1886252573

   benchmark:
   ```Rust
   use arrow::array::{Array, BooleanArray};
   use arrow::compute::{not, and};
   use arrow_buffer::{NullBuffer, BooleanBuffer, bitwise_bin_op_helper};
   use arrow_schema::ArrowError;
   use criterion::{criterion_group, criterion_main, Criterion, black_box};
   use rand::prelude::*;
   
   fn do_bench(c: &mut Criterion, array_length: usize, lhs: &BooleanArray, rhs: 
&BooleanArray) {
       
   
       c.bench_function(&format!("Use and(lhs, not(rhs)), {array_length}"), |b| 
{
           b.iter(|| black_box(and(lhs, &not(rhs).unwrap())))
       });
   
       c.bench_function(&format!("Use and_not(lhs, rhs), {array_length}"), |b| {
           b.iter(|| black_box(and_not(lhs, rhs)))
       });
   }
   
   pub fn and_not(left: &BooleanArray, right: &BooleanArray) -> 
Result<BooleanArray, ArrowError> {
       let nulls = NullBuffer::union(left.nulls(), right.nulls());
       let buffer = bitwise_bin_op_helper(
           left.values().inner(),
           0,
           right.values().inner(),
           0,
           left.len(),
           |a, b| a & !b,
       );
       let values = BooleanBuffer::new(buffer, 0, left.len());
       Ok(BooleanArray::new(values, nulls))
   }
   
   fn do_benches(
       c: &mut Criterion,
       array_length: usize,
   ) {
       let mut rng = StdRng::seed_from_u64(120320);
   
   
       let lhs: BooleanArray = (0..array_length)
           .map(|_| Some(rng.gen::<bool>()))
           .collect();
   
       let rhs: BooleanArray = (0..array_length)
           .map(|_| Some(rng.gen()))
           .collect();
   
       do_bench(
           c,
           array_length,
           &lhs,
           &rhs,
       );
   }
   
   fn criterion_benchmark(c: &mut Criterion) {
       for array_length in [100, 1000, 10000, 100000] {
           do_benches(c, array_length)
       }
   }
   
   criterion_group!(benches, criterion_benchmark);
   criterion_main!(benches);
   ```
   The benchmark result:
   
   | array_length | and(lhs, not(rhs)) | and_not(lhs, rhs) | Improvement Ratio|
   |--------------|-------------------|------------------|------------------|
   | 100 | 149.17 ns | 102.88 ns | 31.11% |
   | 1000 | 167.16 ns | 78.770 ns | 52.89% |
   | 10000 | 270.81 ns | 90.285 ns | 66.67% |
   | 100000 | 666.20 ns | 336.37 ns | 49.51% |
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to