File: higher-order.rs

package info (click to toggle)
rust-ndarray 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,028 kB
  • sloc: sh: 30; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,148 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
#![feature(test)]
#![allow(
    clippy::many_single_char_names, clippy::deref_addrof, clippy::unreadable_literal, clippy::many_single_char_names
)]
extern crate test;
use test::black_box;
use test::Bencher;

use ndarray::prelude::*;

const N: usize = 1024;
const X: usize = 64;
const Y: usize = 16;

#[cfg(feature = "std")]
#[bench]
fn map_regular(bench: &mut Bencher)
{
    let a = Array::linspace(0., 127., N)
        .into_shape_with_order((X, Y))
        .unwrap();
    bench.iter(|| a.map(|&x| 2. * x));
}

pub fn double_array(mut a: ArrayViewMut2<'_, f64>)
{
    a *= 2.0;
}

#[cfg(feature = "std")]
#[bench]
fn map_stride_double_f64(bench: &mut Bencher)
{
    let mut a = Array::linspace(0., 127., N * 2)
        .into_shape_with_order([X, Y * 2])
        .unwrap();
    let mut av = a.slice_mut(s![.., ..;2]);
    bench.iter(|| {
        double_array(av.view_mut());
    });
}

#[cfg(feature = "std")]
#[bench]
fn map_stride_f64(bench: &mut Bencher)
{
    let a = Array::linspace(0., 127., N * 2)
        .into_shape_with_order([X, Y * 2])
        .unwrap();
    let av = a.slice(s![.., ..;2]);
    bench.iter(|| av.map(|&x| 2. * x));
}

#[cfg(feature = "std")]
#[bench]
fn map_stride_u32(bench: &mut Bencher)
{
    let a = Array::linspace(0., 127., N * 2)
        .into_shape_with_order([X, Y * 2])
        .unwrap();
    let b = a.mapv(|x| x as u32);
    let av = b.slice(s![.., ..;2]);
    bench.iter(|| av.map(|&x| 2 * x));
}

#[cfg(feature = "std")]
#[bench]
fn fold_axis(bench: &mut Bencher)
{
    let a = Array::linspace(0., 127., N * 2)
        .into_shape_with_order([X, Y * 2])
        .unwrap();
    bench.iter(|| a.fold_axis(Axis(0), 0., |&acc, &elt| acc + elt));
}

const MA: usize = 64;
const MASZ: usize = MA * MA;

#[bench]
fn map_axis_0(bench: &mut Bencher)
{
    let a = Array::from_iter(0..MASZ as i32)
        .into_shape_with_order([MA, MA])
        .unwrap();
    bench.iter(|| a.map_axis(Axis(0), black_box));
}

#[bench]
fn map_axis_1(bench: &mut Bencher)
{
    let a = Array::from_iter(0..MASZ as i32)
        .into_shape_with_order([MA, MA])
        .unwrap();
    bench.iter(|| a.map_axis(Axis(1), black_box));
}