|
| 1 | +#![feature(test)] |
| 2 | +extern crate test; |
| 3 | +use test::{black_box, Bencher}; |
| 4 | +use ndarray::{Array3, ShapeBuilder, Zip}; |
| 5 | + |
| 6 | +pub fn zip_copy(data: &Array3<f32>, out: &mut Array3<f32>) { |
| 7 | + Zip::from(data).and(out).apply(|&i, o| { |
| 8 | + *o = i; |
| 9 | + }); |
| 10 | +} |
| 11 | + |
| 12 | +pub fn zip_indexed(data: &Array3<f32>, out: &mut Array3<f32>) { |
| 13 | + Zip::indexed(data).and(out).apply(|idx, &i, o| { |
| 14 | + *o = i; |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +pub fn zip_mut_with(data: &Array3<f32>, out: &mut Array3<f32>) { |
| 19 | + out.zip_mut_with(&data, |o, &i| { |
| 20 | + *o = i; |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +// array size in benchmarks |
| 25 | +const SZ3: (usize, usize, usize) = (137, 171, 151); |
| 26 | + |
| 27 | +#[bench] |
| 28 | +fn zip_cf(b: &mut Bencher) { |
| 29 | + let data: Array3<f32> = Array3::zeros(SZ3); |
| 30 | + let mut out = Array3::zeros(data.dim().f()); |
| 31 | + b.iter(|| black_box(zip_copy(&data, &mut out))); |
| 32 | +} |
| 33 | + |
| 34 | +#[bench] |
| 35 | +fn zip_cc(b: &mut Bencher) { |
| 36 | + let data: Array3<f32> = Array3::zeros(SZ3); |
| 37 | + let mut out = Array3::zeros(data.dim()); |
| 38 | + b.iter(|| black_box(zip_copy(&data, &mut out))); |
| 39 | +} |
| 40 | + |
| 41 | +#[bench] |
| 42 | +fn zip_fc(b: &mut Bencher) { |
| 43 | + let data: Array3<f32> = Array3::zeros(SZ3.f()); |
| 44 | + let mut out = Array3::zeros(data.dim()); |
| 45 | + b.iter(|| black_box(zip_copy(&data, &mut out))); |
| 46 | +} |
| 47 | + |
| 48 | +#[bench] |
| 49 | +fn zip_ff(b: &mut Bencher) { |
| 50 | + let data: Array3<f32> = Array3::zeros(SZ3.f()); |
| 51 | + let mut out = Array3::zeros(data.dim().f()); |
| 52 | + b.iter(|| black_box(zip_copy(&data, &mut out))); |
| 53 | +} |
| 54 | + |
| 55 | +#[bench] |
| 56 | +fn zip_indexed_cf(b: &mut Bencher) { |
| 57 | + let data: Array3<f32> = Array3::zeros(SZ3); |
| 58 | + let mut out = Array3::zeros(data.dim().f()); |
| 59 | + b.iter(|| black_box(zip_indexed(&data, &mut out))); |
| 60 | +} |
| 61 | + |
| 62 | +#[bench] |
| 63 | +fn zip_indexed_cc(b: &mut Bencher) { |
| 64 | + let data: Array3<f32> = Array3::zeros(SZ3); |
| 65 | + let mut out = Array3::zeros(data.dim()); |
| 66 | + b.iter(|| black_box(zip_indexed(&data, &mut out))); |
| 67 | +} |
| 68 | + |
| 69 | +#[bench] |
| 70 | +fn zip_indexed_fc(b: &mut Bencher) { |
| 71 | + let data: Array3<f32> = Array3::zeros(SZ3.f()); |
| 72 | + let mut out = Array3::zeros(data.dim()); |
| 73 | + b.iter(|| black_box(zip_indexed(&data, &mut out))); |
| 74 | +} |
| 75 | + |
| 76 | +#[bench] |
| 77 | +fn zip_indexed_ff(b: &mut Bencher) { |
| 78 | + let data: Array3<f32> = Array3::zeros(SZ3.f()); |
| 79 | + let mut out = Array3::zeros(data.dim().f()); |
| 80 | + b.iter(|| black_box(zip_indexed(&data, &mut out))); |
| 81 | +} |
| 82 | + |
| 83 | +#[bench] |
| 84 | +fn zip_mut_with_cf(b: &mut Bencher) { |
| 85 | + let data: Array3<f32> = Array3::zeros(SZ3); |
| 86 | + let mut out = Array3::zeros(data.dim().f()); |
| 87 | + b.iter(|| black_box(zip_mut_with(&data, &mut out))); |
| 88 | +} |
| 89 | + |
| 90 | +#[bench] |
| 91 | +fn zip_mut_with_cc(b: &mut Bencher) { |
| 92 | + let data: Array3<f32> = Array3::zeros(SZ3); |
| 93 | + let mut out = Array3::zeros(data.dim()); |
| 94 | + b.iter(|| black_box(zip_mut_with(&data, &mut out))); |
| 95 | +} |
| 96 | + |
| 97 | +#[bench] |
| 98 | +fn zip_mut_with_fc(b: &mut Bencher) { |
| 99 | + let data: Array3<f32> = Array3::zeros(SZ3.f()); |
| 100 | + let mut out = Array3::zeros(data.dim()); |
| 101 | + b.iter(|| black_box(zip_mut_with(&data, &mut out))); |
| 102 | +} |
| 103 | + |
| 104 | +#[bench] |
| 105 | +fn zip_mut_with_ff(b: &mut Bencher) { |
| 106 | + let data: Array3<f32> = Array3::zeros(SZ3.f()); |
| 107 | + let mut out = Array3::zeros(data.dim().f()); |
| 108 | + b.iter(|| black_box(zip_mut_with(&data, &mut out))); |
| 109 | +} |
0 commit comments