forked from rust-ndarray/ndarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunks.rs
82 lines (75 loc) · 2.17 KB
/
chunks.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#![feature(test)]
extern crate test;
use test::Bencher;
use ndarray::prelude::*;
use ndarray::NdProducer;
#[bench]
fn chunk2x2_iter_sum(bench: &mut Bencher) {
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let mut sum = Array::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
azip!((a in a.exact_chunks(chunksz), sum in &mut sum) {
*sum = a.iter().sum::<f32>();
});
});
}
#[bench]
fn chunk2x2_sum(bench: &mut Bencher) {
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let mut sum = Array::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
azip!((a in a.exact_chunks(chunksz), sum in &mut sum) {
*sum = a.sum();
});
});
}
#[bench]
fn chunk2x2_sum_get1(bench: &mut Bencher) {
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let mut sum = Array::<f32, _>::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
let (m, n) = a.dim();
for i in 0..m {
for j in 0..n {
sum[[i / 2, j / 2]] += a[[i, j]];
}
}
});
}
#[bench]
fn chunk2x2_sum_uget1(bench: &mut Bencher) {
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let mut sum = Array::<f32, _>::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
let (m, n) = a.dim();
for i in 0..m {
for j in 0..n {
unsafe {
*sum.uget_mut([i / 2, j / 2]) += *a.uget([i, j]);
}
}
}
});
}
#[bench]
#[allow(clippy::identity_op)]
fn chunk2x2_sum_get2(bench: &mut Bencher) {
let a = Array::<f32, _>::zeros((256, 256));
let chunksz = (2, 2);
let mut sum = Array::<f32, _>::zeros(a.exact_chunks(chunksz).raw_dim());
bench.iter(|| {
let (m, n) = sum.dim();
for i in 0..m {
for j in 0..n {
sum[[i, j]] += a[[i * 2 + 0, j * 2 + 0]];
sum[[i, j]] += a[[i * 2 + 0, j * 2 + 1]];
sum[[i, j]] += a[[i * 2 + 1, j * 2 + 1]];
sum[[i, j]] += a[[i * 2 + 1, j * 2 + 0]];
}
}
});
}