use criterion::*; use adtensor::{Vector}; use std::ops::{Add, AddAssign, Mul, Deref}; use std::mem; use std::iter::{FromIterator, Iterator, IntoIterator, repeat}; use typenum::*; use generic_array::{GenericArray, ArrayLength}; use ndarray::{Array1}; use nalgebra as na; fn vec_zeros() -> Vec where T: Default + Clone, N: Unsigned { let mut v = Vec::::default(); v.resize(::to_usize(), T::default()); v } fn array1_zeros() -> Array1 where T: Default + Clone, N: Unsigned { Array1::::from_vec(vec_zeros::()) } struct Raw { t: T } impl Default for Raw where T: Default { fn default() -> Self { Self{t: T::default()} } } impl Clone for Raw where T: Clone { fn clone(&self) -> Self { Self{t: self.t.clone()} } } impl Copy for Raw where T: Copy {} impl Add> for Raw where T: Add { type Output = Raw; fn add(self, rhs: Self) -> Self { Self{t: self.t + rhs.t} } } impl AddAssign> for Raw where T: AddAssign { fn add_assign(&mut self, rhs: Self) { self.t += rhs.t; } } impl Mul> for Raw where T: Mul { type Output = Raw; fn mul(self, rhs: Self) -> Self { Self{t: self.t * rhs.t} } } fn criterion_benchmark(c: &mut Criterion) { type T = f32; type N = U100; type M = na::U100; c.bench_function("vector_raw_dot", |b| b.iter(|| { let x = Vector::, N>::default(); let y = Vector::, N>::default(); x.dot(&y) })); c.bench_function("vector_dot", |b| b.iter(|| { let x = Vector::::default(); let y = Vector::::default(); x.dot(&y) })); /*c.bench_function("na_arr_dot", |b| b.iter(|| { let x = na::Matrix::>::from_iterator(repeat(0.1)); let y = na::Matrix::>::from_iterator(repeat(0.1)); x.dot(&y) })); c.bench_function("array1_dot", |b| b.iter(|| { let x = Array1::from_iter(repeat(0.1).take(N::to_usize())); let y = Array1::from_iter(repeat(0.1).take(N::to_usize())); x.dot(&y) })); c.bench_function("vector_map", |b| b.iter(|| { let x = Vector::::from_iter(repeat(0.1)); let w = x.map(|t| (t+1.0)/(t-1.0)); w[0] })); c.bench_function("vector_apply", |b| b.iter(|| { let mut x = Vector::::from_iter(repeat(0.1)); x.apply(|t| (t+1.0)/(t-1.0)); x[0] })); c.bench_function("na_apply", |b| b.iter(|| { let mut x = na::Matrix::>::from_iterator(repeat(0.1)); x.apply(|t| (t+1.0)/(t-1.0)); x[0] })); c.bench_function("array1_apply", |b| b.iter(|| { let mut x = Array1::from_iter(repeat(0.1).take(N::to_usize())); for i in 0..x.len() { x[i] = (x[i]+1.0)/(x[i]-1.0); } x[0] }));*/ } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);