Enhance benchmarks by expanding size tests to include 1, 100, and 1000 matrices, and change Criterion config

This commit is contained in:
Palash Tyagi 2025-05-03 14:59:48 +01:00
parent 4e70e868fd
commit 676ae413a6

View File

@ -1,15 +1,33 @@
// Combined benchmarks for rustframe // Combined benchmarks for rustframe
use chrono::NaiveDate; use chrono::NaiveDate;
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
// Import Duration for measurement_time and warm_up_time
use rustframe::{ use rustframe::{
frame::{Frame, RowIndex}, frame::{Frame, RowIndex},
matrix::{BoolMatrix, Matrix}, matrix::{BoolMatrix, Matrix},
utils::{BDateFreq, BDatesList}, utils::{BDateFreq, BDatesList},
}; };
use std::time::Duration;
// You can define a custom Criterion configuration function
// This will be passed to the criterion_group! macro
pub fn for_short_runs() -> Criterion {
Criterion::default()
// (samples != total iterations)
// limits the number of statistical data points.
.sample_size(50)
// measurement time per sample
.measurement_time(Duration::from_millis(250))
// reduce warm-up time as well for faster overall run
.warm_up_time(Duration::from_millis(50))
// You could also make it much shorter if needed, e.g., 50ms measurement, 100ms warm-up
// .measurement_time(Duration::from_millis(50))
// .warm_up_time(Duration::from_millis(100))
}
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]; // 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 +62,8 @@ 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]; // 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 +98,8 @@ 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 sizes = [1, 100, 1000];
let sizes = [1000]; // let sizes = [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();
@ -146,17 +164,21 @@ fn matrix_operations_benchmark(c: &mut Criterion) {
} }
fn benchmark_frame_operations(c: &mut Criterion) { fn benchmark_frame_operations(c: &mut Criterion) {
let n_periods = 4; let n_periods = 1000;
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("2024-01-02".to_string(), BDateFreq::Daily, n_periods)
.unwrap() .unwrap()
.list() .list()
.unwrap(); .unwrap();
let col_names: Vec<String> = vec!["a".to_string(), "b".to_string()]; // 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 ma = Matrix::from_cols(vec![vec![1.0, 2.0, 3.0, 4.0], vec![5.0, 6.0, 7.0, 8.0]]); let data1: Vec<f64> = (0..n_periods * n_cols).map(|x| x as f64).collect();
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 data2: Vec<f64> = (0..n_periods * n_cols).map(|x| (x + 1) as f64).collect();
let ma = Matrix::from_vec(data1.clone(), n_periods, n_cols);
let mb = Matrix::from_vec(data2.clone(), n_periods, n_cols);
let fa = Frame::new( let fa = Frame::new(
ma.clone(), ma.clone(),
@ -165,18 +187,20 @@ fn benchmark_frame_operations(c: &mut Criterion) {
); );
let fb = Frame::new(mb, col_names, Some(RowIndex::Date(dates))); let fb = Frame::new(mb, col_names, Some(RowIndex::Date(dates)));
c.bench_function("frame element-wise multiply", |b| { c.bench_function("frame element-wise multiply (1000x1000)", |b| {
b.iter(|| { b.iter(|| {
let _result = &fa * &fb; let _result = &fa * &fb;
}); });
}); });
} }
// Define the criterion group and pass the custom configuration function
criterion_group!( criterion_group!(
combined_benches, name = combined_benches;
bool_matrix_operations_benchmark, config = for_short_runs(); // Use the custom configuration here
matrix_boolean_operations_benchmark, targets = bool_matrix_operations_benchmark,
matrix_operations_benchmark, matrix_boolean_operations_benchmark,
benchmark_frame_operations matrix_operations_benchmark,
benchmark_frame_operations
); );
criterion_main!(combined_benches); criterion_main!(combined_benches);