Compare commits

..

1 Commits

View File

@ -1,15 +1,14 @@
// Combined benchmarks for rustframe // Combined benchmarks for rustframe
use chrono::NaiveDate;
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
use rustframe::{ use rustframe::{
frame::{Frame, RowIndex}, frame::{Frame, RowIndex},
matrix::{BoolMatrix, Matrix}, matrix::{BoolMatrix, Matrix},
utils::{BDateFreq, BDatesList}, utils::{BDateFreq, BDatesList},
}; };
use chrono::NaiveDate;
fn bool_matrix_operations_benchmark(c: &mut Criterion) { 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 { for &size in &sizes {
let data1: Vec<bool> = (0..size * size).map(|x| x % 2 == 0).collect(); let data1: Vec<bool> = (0..size * size).map(|x| x % 2 == 0).collect();
@ -44,8 +43,7 @@ fn bool_matrix_operations_benchmark(c: &mut Criterion) {
} }
fn matrix_boolean_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 { for &size in &sizes {
let data1: Vec<bool> = (0..size * size).map(|x| x % 2 == 0).collect(); let data1: Vec<bool> = (0..size * size).map(|x| x % 2 == 0).collect();
@ -80,8 +78,32 @@ fn matrix_boolean_operations_benchmark(c: &mut Criterion) {
} }
fn matrix_operations_benchmark(c: &mut Criterion) { fn matrix_operations_benchmark(c: &mut Criterion) {
// let sizes = [1, 100, 1000]; let n_periods = 4;
let sizes = [1000]; let dates: Vec<NaiveDate> =
BDatesList::from_n_periods("2024-01-02".to_string(), BDateFreq::Daily, n_periods)
.unwrap()
.list()
.unwrap();
let col_names: Vec<String> = 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];
for &size in &sizes { for &size in &sizes {
let data: Vec<f64> = (0..size * size).map(|x| x as f64).collect(); let data: Vec<f64> = (0..size * size).map(|x| x as f64).collect();
@ -111,72 +133,7 @@ fn matrix_operations_benchmark(c: &mut Criterion) {
}); });
}); });
} }
// Benchmarking matrix addition
for &size in &sizes {
let data1: Vec<f64> = (0..size * size).map(|x| x as f64).collect();
let data2: Vec<f64> = (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;
});
});
}
} }
fn benchmark_frame_operations(c: &mut Criterion) { criterion_group!(combined_benches, bool_matrix_operations_benchmark, matrix_boolean_operations_benchmark, matrix_operations_benchmark);
let n_periods = 4;
let dates: Vec<NaiveDate> =
BDatesList::from_n_periods("2024-01-02".to_string(), BDateFreq::Daily, n_periods)
.unwrap()
.list()
.unwrap();
let col_names: Vec<String> = 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); criterion_main!(combined_benches);