added main
This commit is contained in:
parent
c859a6f0ea
commit
7185e91cc4
154
src/main.rs
Normal file
154
src/main.rs
Normal file
@ -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<String> {
|
||||
(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<String>, dates_vec: Vec<NaiveDate>) -> Frame<f64> {
|
||||
let num_cols = column_labels.len();
|
||||
let num_rows = dates_vec.len();
|
||||
|
||||
let mut data: Vec<Vec<f64>> = Vec::with_capacity(num_cols);
|
||||
for _ in 0..num_cols {
|
||||
let col: Vec<f64> = (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<NaiveDate> = dates_list.list().unwrap().to_vec();
|
||||
let column_labels: Vec<String> = 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
|
||||
);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user