Enhance frame benchmarks by adding arithmetic operations and refactoring frame generation

This commit is contained in:
Palash Tyagi 2025-05-07 00:08:28 +01:00
parent 894b85b384
commit 34809656f6

View File

@ -4,7 +4,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use rustframe::{ use rustframe::{
frame::{Frame, RowIndex}, frame::{Frame, RowIndex},
matrix::{BoolMatrix, Matrix}, matrix::{BoolMatrix, Matrix, SeriesOps},
utils::{BDateFreq, BDatesList}, utils::{BDateFreq, BDatesList},
}; };
use std::time::Duration; use std::time::Duration;
@ -160,35 +160,76 @@ fn matrix_operations_benchmark(c: &mut Criterion) {
} }
} }
fn benchmark_frame_operations(c: &mut Criterion) { fn generate_frame(size: usize) -> Frame<f64> {
let n_periods = 1000; let data: Vec<f64> = (0..size * size).map(|x| x as f64).collect();
let n_cols = 1000;
let dates: Vec<NaiveDate> = let dates: Vec<NaiveDate> =
BDatesList::from_n_periods("2024-01-02".to_string(), BDateFreq::Daily, n_periods) BDatesList::from_n_periods("2000-01-01".to_string(), BDateFreq::Daily, size)
.unwrap() .unwrap()
.list() .list()
.unwrap(); .unwrap();
// let col_names= str(i) for i in range(1, 1000) // let col_names= str(i) for i in range(1, 1000)
let col_names: Vec<String> = (1..=n_cols).map(|i| format!("col_{}", i)).collect(); let col_names: Vec<String> = (1..=size).map(|i| format!("col_{}", i)).collect();
let data1: Vec<f64> = (0..n_periods * n_cols).map(|x| x as f64).collect(); Frame::new(
let data2: Vec<f64> = (0..n_periods * n_cols).map(|x| (x + 1) as f64).collect(); Matrix::from_vec(data.clone(), size, size),
let ma = Matrix::from_vec(data1.clone(), n_periods, n_cols); col_names,
let mb = Matrix::from_vec(data2.clone(), n_periods, n_cols); Some(RowIndex::Date(dates)),
)
}
let fa = Frame::new( fn benchmark_frame_operations(c: &mut Criterion) {
ma.clone(), let sizes = BENCH_SIZES;
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 (1000x1000)", |b| { for &size in &sizes {
b.iter(|| { let fa = generate_frame(size);
let _result = &fa * &fb; let fb = generate_frame(size);
c.bench_function(&format!("frame add ({}x{})", size, size), |b| {
b.iter(|| {
let _result = &fa + &fb;
});
}); });
});
c.bench_function(&format!("frame subtract ({}x{})", size, size), |b| {
b.iter(|| {
let _result = &fa - &fb;
});
});
c.bench_function(&format!("frame multiply ({}x{})", size, size), |b| {
b.iter(|| {
let _result = &fa * &fb;
});
});
c.bench_function(&format!("frame divide ({}x{})", size, size), |b| {
b.iter(|| {
let _result = &fa / &fb;
});
});
c.bench_function(&format!("frame sum_horizontal ({}x{})", size, size), |b| {
b.iter(|| {
let _result = fa.sum_horizontal();
});
});
c.bench_function(&format!("frame sum_vertical ({}x{})", size, size), |b| {
b.iter(|| {
let _result = fa.sum_vertical();
});
});
c.bench_function(&format!("frame prod_horizontal ({}x{})", size, size), |b| {
b.iter(|| {
let _result = fa.prod_horizontal();
});
});
c.bench_function(&format!("frame prod_vertical ({}x{})", size, size), |b| {
b.iter(|| {
let _result = fa.prod_vertical();
});
});
}
} }
// Define the criterion group and pass the custom configuration function // Define the criterion group and pass the custom configuration function