Skip to content

Commit

Permalink
Use Zip instead of multiplication operator
Browse files Browse the repository at this point in the history
This avoids cloning the left array. It results in a substantial
performance improvement (30-50%).
  • Loading branch information
jturner314 committed Apr 13, 2018
1 parent 4bf2eec commit 6ec9a05
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions benches/bench_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ extern crate ndarray;
extern crate rand;

use criterion::Criterion;
use ndarray::{Array, ArrayBase, ArrayD, ArrayViewD, Axis};
use ndarray::prelude::*;
use ndarray::Zip;
use rand::{Rand, Rng};

/// Returns an array view with `n_axis` axes with length 1 at the start of the shape.
Expand Down Expand Up @@ -53,9 +54,11 @@ fn product(c: &mut Criterion) {
(left_view, right_view, expected_card.clone())
},
|(left_view, right_view, expected_card)| {
// Broadcasting the left hand side first.
let left_view = left_view.broadcast(expected_card).unwrap();
let _array = &left_view * &right_view;
let mut out = unsafe { ArrayD::<f64>::uninitialized(expected_card) };
Zip::from(&mut out)
.and_broadcast(&left_view)
.and_broadcast(&right_view)
.apply(|out, a, b| *out = a * b);
},
)
},
Expand Down

0 comments on commit 6ec9a05

Please sign in to comment.