Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode expected/actual info in ShapeError #962

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
TEST: Add benchmarks for Result<_, ShapeError> methods
  • Loading branch information
bluss committed Mar 29, 2021
commit da365be33b6b7fbe60e63aaf2e8b3dc0671ddbe8
110 changes: 110 additions & 0 deletions benches/error-handling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#![feature(test)]
#![allow(
clippy::many_single_char_names,
clippy::deref_addrof,
clippy::unreadable_literal,
clippy::many_single_char_names
)]
extern crate test;
use test::Bencher;

use ndarray::prelude::*;
use ndarray::ErrorKind;

// Use ZST elements to remove allocation from the benchmarks

#[derive(Copy, Clone, Debug)]
struct Zst;

type A4 = Array4<Zst>;

#[bench]
fn from_elem(bench: &mut Bencher) {
bench.iter(|| {
A4::from_elem((1, 2, 3, 4), Zst)
})
}

#[bench]
fn from_shape_vec_ok(bench: &mut Bencher) {
bench.iter(|| {
let v: Vec<Zst> = vec![Zst; 1 * 2 * 3 * 4];
let x = A4::from_shape_vec((1, 2, 3, 4).strides((24, 12, 4, 1)), v);
debug_assert!(x.is_ok(), "problem with {:?}", x);
x
})
}

#[bench]
fn from_shape_vec_fail(bench: &mut Bencher) {
bench.iter(|| {
let v: Vec<Zst> = vec![Zst; 1 * 2 * 3 * 4];
let x = A4::from_shape_vec((1, 2, 3, 4).strides((4, 3, 2, 1)), v);
debug_assert!(x.is_err());
x
})
}

#[bench]
fn into_shape_fail(bench: &mut Bencher) {
let a = A4::from_elem((1, 2, 3, 4), Zst);
let v = a.view();
bench.iter(|| {
v.clone().into_shape((5, 3, 2, 1))
})
}

#[bench]
fn into_shape_ok_c(bench: &mut Bencher) {
let a = A4::from_elem((1, 2, 3, 4), Zst);
let v = a.view();
bench.iter(|| {
v.clone().into_shape((4, 3, 2, 1))
})
}

#[bench]
fn into_shape_ok_f(bench: &mut Bencher) {
let a = A4::from_elem((1, 2, 3, 4).f(), Zst);
let v = a.view();
bench.iter(|| {
v.clone().into_shape((4, 3, 2, 1))
})
}

#[bench]
fn stack_ok(bench: &mut Bencher) {
let a = Array::from_elem((15, 15), Zst);
let rows = a.rows().into_iter().collect::<Vec<_>>();
bench.iter(|| {
let res = ndarray::stack(Axis(1), &rows);
debug_assert!(res.is_ok(), "err {:?}", res);
res
});
}

#[bench]
fn stack_err_axis(bench: &mut Bencher) {
let a = Array::from_elem((15, 15), Zst);
let rows = a.rows().into_iter().collect::<Vec<_>>();
bench.iter(|| {
let res = ndarray::stack(Axis(2), &rows);
debug_assert!(res.is_err());
res
});
}

#[bench]
fn stack_err_shape(bench: &mut Bencher) {
let a = Array::from_elem((15, 15), Zst);
let rows = a.rows().into_iter()
.enumerate()
.map(|(i, mut row)| { row.slice_collapse(s![..(i as isize)]); row })
.collect::<Vec<_>>();
bench.iter(|| {
let res = ndarray::stack(Axis(1), &rows);
debug_assert!(res.is_err());
debug_assert_eq!(res.clone().unwrap_err().kind(), ErrorKind::IncompatibleShape);
res
});
}