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