From 7185e91cc4dc90749c3085d8cfc59672f944a422 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:36:51 +0100 Subject: [PATCH] added main --- src/main.rs | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..75a25fa --- /dev/null +++ b/src/main.rs @@ -0,0 +1,154 @@ +use chrono::NaiveDate; +use rand::distr::Uniform; +use rand::{Rng, rng}; // Import thread_rng for random number generation +use rustframe::frame::{Frame, RowIndex}; +use rustframe::matrix::{BoolMatrix, BoolOps, Matrix, SeriesOps}; // Explicitly list used items +use rustframe::utils::{BDateFreq, BDatesList}; +use std::time::Instant; // Use Instant for timing + +// Helper function to generate a random f64 between 0.0 and 1.0 +fn generate_random_float() -> f64 { + let mut rng = rng(); // Get the thread-local random number generator + let uniform = Uniform::new(0.0, 1.0).unwrap(); // Define a uniform distribution range and unwrap the result + rng.sample(&uniform) // Sample a value from the distribution +} + +// Helper function to generate column labels +fn generate_column_labels(num_cols: usize) -> Vec { + (0..num_cols).map(|i| format!("col_{}", i)).collect() // Use "col" prefix +} + +// Helper function to generate a Frame with random data +fn generate_random_frame(column_labels: Vec, dates_vec: Vec) -> Frame { + let num_cols = column_labels.len(); + let num_rows = dates_vec.len(); + + let mut data: Vec> = Vec::with_capacity(num_cols); + for _ in 0..num_cols { + let col: Vec = (0..num_rows).map(|_| generate_random_float()).collect(); + data.push(col); + } + + let matrix = Matrix::from_cols(data); // Create Matrix from columns + + Frame::new( + matrix, + column_labels, // Consume the vector of labels + Some(RowIndex::Date(dates_vec)), // Consume the vector of dates for the index + ) +} + +fn main() { + // Start overall timing + let total_start_time = Instant::now(); + + // --- Configuration --- + let start_date_str = "2000-01-01".to_string(); + let num_periods = 20_000; + let num_columns = 1_000; + let frequency = BDateFreq::Daily; + + println!("--- Demo Parameters ---"); + println!("Start Date: {}", start_date_str); + println!("Number of Periods (Dates): {}", num_periods); + println!("Number of Columns: {}", num_columns); + println!("Frequency: {:?}", frequency); + println!("-----------------------"); + println!("--- Timing Results ---"); + + // --- Setup Data (Dates and Labels) --- + let setup_data_start_time = Instant::now(); + + let dates_list = BDatesList::from_n_periods(start_date_str, frequency, num_periods).unwrap(); + let dates_vec: Vec = dates_list.list().unwrap().to_vec(); + let column_labels: Vec = generate_column_labels(num_columns); + + let setup_data_duration = setup_data_start_time.elapsed(); + println!( + "Setup data (dates/labels) duration: {:?}", + setup_data_duration + ); + + // --- Frame Creation --- + let frame_creation_start_time = Instant::now(); + + // Create two frames with the same structure but different random data + // Clone labels and dates because generate_random_frame consumes them + let frame_a = generate_random_frame(column_labels.clone(), dates_vec.clone()); + let frame_b = generate_random_frame(column_labels.clone(), dates_vec.clone()); + + let frame_creation_duration = frame_creation_start_time.elapsed(); + println!( + "Frame size: dates: {}, cols: {}", + frame_a.rows(), + frame_a.cols() + ); + println!("Frame creation duration: {:?}", frame_creation_duration); + + // --- Arithmetic Operations and Timing --- + + // Multiplication + let mul_start_time = Instant::now(); + let _result_mul = &frame_a * &frame_b; // Store result, even if unused, as the operation happens + let mul_duration = mul_start_time.elapsed(); + println!("Multiplication duration: {:?}", mul_duration); + + // Addition + let add_start_time = Instant::now(); + let frame_r = &frame_a + &frame_b; + let add_duration = add_start_time.elapsed(); + println!("Addition duration: {:?}", add_duration); + + // Division (using the result of addition) + let div_start_time = Instant::now(); + let frame_r = &frame_r / &frame_b; + let div_duration = div_start_time.elapsed(); + println!("Division duration: {:?}", div_duration); + + // Subtraction (using the result of division) + let sub_start_time = Instant::now(); + let frame_r = &frame_r - &frame_a; + let sub_duration = sub_start_time.elapsed(); + println!("Subtraction duration: {:?}", sub_duration); + + // --- Boolean Operations and Timing --- + + // Element-wise comparison (e.g., less than) + let bool_mat_start_time = Instant::now(); + // Check elements in the subtraction result that are less than a small value + let frame_r = frame_r.matrix().lt_elementwise(0.001); + let bool_mat_duration = bool_mat_start_time.elapsed(); + println!("LT operation duration: {:?}", bool_mat_duration); + + // Reduction operation (e.g., check if 'any' element is true) + let any_start_time = Instant::now(); + let any_result = frame_r.any(); + let any_duration = any_start_time.elapsed(); + println!("Any operation duration: {:?}", any_duration); + println!("Any operation result: {:?}", any_result); + + // Complex operation + let complex_start_time = Instant::now(); + let frame_r = &frame_a * &frame_b; + let frame_r = &frame_r / &frame_b; + let frame_r = &frame_r - &frame_a; + let frame_r = &frame_r + &frame_a; + let frame_r = &frame_r - &frame_a; + + let frame_r = frame_r.matrix().lt_elementwise(0.0000001); + let complex_result = frame_r.any(); + let complex_duration = complex_start_time.elapsed(); + println!("Complex operation duration: {:?}", complex_duration); + println!( + "Complex operation result (expected true): {:?}", + complex_result + ); + println!("-----------------------"); + + // End overall timing + let total_duration = total_start_time.elapsed(); + println!( + "Total execution duration (including setup and ops): {:?}", + total_duration + ); +}