🔧 Tests and benchmarks #16

Merged
julius merged 6 commits from core-types into master 2024-01-04 11:59:32 +00:00
2 changed files with 41 additions and 36 deletions
Showing only changes of commit bd40c9c1f2 - Show all commits

View File

@ -1,15 +1,15 @@
use manifold::*;
use rand::Rng;
use criterion::Throughput; use criterion::Throughput;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use manifold::*;
use rand::Rng;
fn random_tensor_r2_manifold() -> Tensor<f64, 2> { fn random_tensor_r2_manifold() -> Tensor<f64, 2> {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut tensor = tensor!([[0.0; 1000]; 1000]); let mut tensor = tensor!([[0.0; 1000]; 1000]);
for i in 0..tensor.len() { for i in 0..tensor.len() {
tensor[i] = rng.gen(); tensor[i] = rng.gen();
} }
tensor tensor
} }
fn random_tensor_r2_ndarray() -> ndarray::Array2<f64> { fn random_tensor_r2_ndarray() -> ndarray::Array2<f64> {
@ -25,37 +25,42 @@ fn random_tensor_r2_ndarray() -> ndarray::Array2<f64> {
} }
fn tensor_product(c: &mut Criterion) { fn tensor_product(c: &mut Criterion) {
let b = 1000; let b = 1000;
let mut group = c.benchmark_group("element-wise addition"); let mut group = c.benchmark_group("element-wise addition");
for (i, size) in [b].iter().enumerate() { for (i, size) in [b].iter().enumerate() {
group.throughput(Throughput::Elements(*size as u64)); group.throughput(Throughput::Elements(*size as u64));
group.bench_with_input(BenchmarkId::new("manifold", size), &i, |b, _| { group.bench_with_input(
b.iter(|| { BenchmarkId::new("manifold", size),
let a = random_tensor_r2_manifold(); &i,
let b = random_tensor_r2_manifold(); |b, _| {
let c = a + b; b.iter(|| {
assert!(c.shape().as_array() == &[1000, 1000]); let a = random_tensor_r2_manifold();
}) let b = random_tensor_r2_manifold();
}); let c = a + b;
assert!(c.shape().as_array() == &[1000, 1000]);
})
},
);
group.bench_with_input(BenchmarkId::new("ndarray", size), &i, |b, _| { group.bench_with_input(
b.iter(|| { BenchmarkId::new("ndarray", size),
let a = random_tensor_r2_ndarray(); &i,
let b = random_tensor_r2_ndarray(); |b, _| {
let c = a + b; b.iter(|| {
assert!(c.shape() == &[1000, 1000]); let a = random_tensor_r2_ndarray();
}) let b = random_tensor_r2_ndarray();
}); let c = a + b;
} assert!(c.shape() == &[1000, 1000]);
group.finish(); })
},
);
}
group.finish();
} }
criterion_group!( criterion_group!(benches, tensor_product);
benches,
tensor_product
);
criterion_main!(benches); criterion_main!(benches);

View File

@ -1 +1 @@
mod basic_tests; mod basic_tests;