From 4e70e868fdcd3250c27a29df4c764f8f25464116 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 3 May 2025 02:21:15 +0100 Subject: [PATCH] Refactor benchmarks to focus on 1000 size matrices and add matrix arithmetic operations --- benches/benchmarks.rs | 105 +++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 31 deletions(-) diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index cd4cf9f..56f6b23 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -1,14 +1,15 @@ // Combined benchmarks for rustframe +use chrono::NaiveDate; use criterion::{criterion_group, criterion_main, Criterion}; use rustframe::{ frame::{Frame, RowIndex}, matrix::{BoolMatrix, Matrix}, utils::{BDateFreq, BDatesList}, }; -use chrono::NaiveDate; fn bool_matrix_operations_benchmark(c: &mut Criterion) { - let sizes = [1, 100, 1000]; + // let sizes = [1, 100, 1000]; + let sizes = [1000]; for &size in &sizes { let data1: Vec = (0..size * size).map(|x| x % 2 == 0).collect(); @@ -43,7 +44,8 @@ fn bool_matrix_operations_benchmark(c: &mut Criterion) { } fn matrix_boolean_operations_benchmark(c: &mut Criterion) { - let sizes = [1, 100, 1000]; + // let sizes = [1, 100, 1000]; + let sizes = [1000]; for &size in &sizes { let data1: Vec = (0..size * size).map(|x| x % 2 == 0).collect(); @@ -78,32 +80,8 @@ fn matrix_boolean_operations_benchmark(c: &mut Criterion) { } fn matrix_operations_benchmark(c: &mut Criterion) { - let n_periods = 4; - let dates: Vec = - BDatesList::from_n_periods("2024-01-02".to_string(), BDateFreq::Daily, n_periods) - .unwrap() - .list() - .unwrap(); - - let col_names: Vec = vec!["a".to_string(), "b".to_string()]; - - let ma = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0, 4.0], vec![5.0, 6.0, 7.0, 8.0]]); - let mb = Matrix::from_cols(vec![vec![4.0, 3.0, 2.0, 1.0], vec![8.0, 7.0, 6.0, 5.0]]); - - let fa = Frame::new( - ma.clone(), - col_names.clone(), - Some(RowIndex::Date(dates.clone())), - ); - let fb = Frame::new(mb, col_names, Some(RowIndex::Date(dates))); - - c.bench_function("element-wise multiply", |b| { - b.iter(|| { - let _result = &fa * &fb; - }); - }); - - let sizes = [1, 100, 1000]; + // let sizes = [1, 100, 1000]; + let sizes = [1000]; for &size in &sizes { let data: Vec = (0..size * size).map(|x| x as f64).collect(); @@ -133,7 +111,72 @@ fn matrix_operations_benchmark(c: &mut Criterion) { }); }); } + + // Benchmarking matrix addition + for &size in &sizes { + let data1: Vec = (0..size * size).map(|x| x as f64).collect(); + let data2: Vec = (0..size * size).map(|x| (x + 1) as f64).collect(); + let ma = Matrix::from_vec(data1.clone(), size, size); + let mb = Matrix::from_vec(data2.clone(), size, size); + + c.bench_function(&format!("matrix add ({}x{})", size, size), |b| { + b.iter(|| { + let _result = &ma + &mb; + }); + }); + + c.bench_function(&format!("matrix subtract ({}x{})", size, size), |b| { + b.iter(|| { + let _result = &ma - &mb; + }); + }); + + c.bench_function(&format!("matrix multiply ({}x{})", size, size), |b| { + b.iter(|| { + let _result = &ma * &mb; + }); + }); + + c.bench_function(&format!("matrix divide ({}x{})", size, size), |b| { + b.iter(|| { + let _result = &ma / &mb; + }); + }); + } } -criterion_group!(combined_benches, bool_matrix_operations_benchmark, matrix_boolean_operations_benchmark, matrix_operations_benchmark); -criterion_main!(combined_benches); \ No newline at end of file +fn benchmark_frame_operations(c: &mut Criterion) { + let n_periods = 4; + let dates: Vec = + BDatesList::from_n_periods("2024-01-02".to_string(), BDateFreq::Daily, n_periods) + .unwrap() + .list() + .unwrap(); + + let col_names: Vec = vec!["a".to_string(), "b".to_string()]; + + let ma = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0, 4.0], vec![5.0, 6.0, 7.0, 8.0]]); + let mb = Matrix::from_cols(vec![vec![4.0, 3.0, 2.0, 1.0], vec![8.0, 7.0, 6.0, 5.0]]); + + let fa = Frame::new( + ma.clone(), + col_names.clone(), + Some(RowIndex::Date(dates.clone())), + ); + let fb = Frame::new(mb, col_names, Some(RowIndex::Date(dates))); + + c.bench_function("frame element-wise multiply", |b| { + b.iter(|| { + let _result = &fa * &fb; + }); + }); +} + +criterion_group!( + combined_benches, + bool_matrix_operations_benchmark, + matrix_boolean_operations_benchmark, + matrix_operations_benchmark, + benchmark_frame_operations +); +criterion_main!(combined_benches);