use scirs2_core::profiling::{Profiler, Timer};
use std::thread;
use std::time::Duration;
#[allow(dead_code)]
fn main() {
println!("Profiling System Example");
#[cfg(feature = "profiling")]
{
Profiler::global().lock().expect("Operation failed").start();
println!("\n--- Basic Timing Example ---");
basic_timing_example();
println!("\n--- Function Timing Example ---");
function_timing_example();
println!("\n--- Hierarchical Timing Example ---");
hierarchical_timing_example();
println!("\n--- Memory Tracking Example ---");
memory_tracking_example();
println!("\n--- Profiling Report ---");
Profiler::global()
.lock()
.expect("Operation failed")
.print_report();
Profiler::global().lock().expect("Operation failed").stop();
}
#[cfg(not(feature = "profiling"))]
println!(
"Profiling feature not enabled. Run with --features=\"profiling\" to see the example."
);
}
#[cfg(feature = "profiling")]
#[allow(dead_code)]
fn basic_timing_example() {
let timer = Timer::start("basic_operation");
println!("Performing a basic operation...");
thread::sleep(Duration::from_millis(500));
timer.stop();
println!("Basic operation completed");
}
#[cfg(feature = "profiling")]
#[allow(dead_code)]
fn function_timing_example() {
let result = Timer::time_function("calculate_result", || {
println!("Calculating result...");
thread::sleep(Duration::from_millis(300));
42 });
println!("Function result: {}", result);
}
#[cfg(feature = "profiling")]
#[allow(dead_code)]
fn hierarchical_timing_example() {
let parent_timer = Timer::start("parent_operation");
println!("Starting parent operation...");
thread::sleep(Duration::from_millis(200));
{
let child1 = Timer::start_with_parent("child_operation_1", "parent_operation");
println!(" Performing child operation 1...");
thread::sleep(Duration::from_millis(300));
child1.stop();
}
{
let child2 = Timer::start_with_parent("child_operation_2", "parent_operation");
println!(" Performing child operation 2...");
thread::sleep(Duration::from_millis(400));
child2.stop();
}
thread::sleep(Duration::from_millis(100));
parent_timer.stop();
println!("Parent operation completed");
}
#[cfg(feature = "profiling")]
#[allow(dead_code)]
fn memory_tracking_example() {
{
let mem_tracker = scirs2_core::profiling::MemoryTracker::start("allocate_vector");
println!("Allocating a vector...");
let large_vector = vec![0; 1_000_000];
println!("Vector size: {} elements", large_vector.len());
mem_tracker.stop();
}
{
let mem_tracker = scirs2_core::profiling::MemoryTracker::start("allocatematrix");
println!("Allocating a matrix...");
let matrix_size = 500;
let matrix = vec![vec![0.0; matrix_size]; matrix_size];
println!("Matrix size: {}x{} elements", matrix.len(), matrix[0].len());
mem_tracker.stop();
}
}