From 6ec9a0508151466aa17a6a5b33c09c20ae595cb1 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Fri, 13 Apr 2018 17:16:28 -0400 Subject: [PATCH] Use Zip instead of multiplication operator This avoids cloning the left array. It results in a substantial performance improvement (30-50%). --- benches/bench_main.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/benches/bench_main.rs b/benches/bench_main.rs index 17d9e3d..bab0ff7 100644 --- a/benches/bench_main.rs +++ b/benches/bench_main.rs @@ -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. @@ -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::::uninitialized(expected_card) }; + Zip::from(&mut out) + .and_broadcast(&left_view) + .and_broadcast(&right_view) + .apply(|out, a, b| *out = a * b); }, ) },