pub struct PerformanceProfiler { /* private fields */ }
Expand description
Performance profiler for neural network operations
Tracks timing information for neural network operations to identify performance bottlenecks and optimize training pipelines.
Implementations§
Source§impl PerformanceProfiler
impl PerformanceProfiler
Sourcepub fn new(enabled: bool) -> Self
pub fn new(enabled: bool) -> Self
Create a new performance profiler
§Arguments
enabled
- Whether profiling is enabled
§Examples
use scirs2_neural::performance::threading::PerformanceProfiler;
let mut profiler = PerformanceProfiler::new(true);
let timer = profiler.start_timer("forward_pass");
// ... perform operation
profiler.end_timer("forward_pass".to_string(), timer);
Sourcepub fn start_timer(&mut self, name: &str) -> Option<Instant>
pub fn start_timer(&mut self, name: &str) -> Option<Instant>
Start timing an operation
Examples found in repository?
examples/new_features_showcase.rs (line 166)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129 println!("⚡ Performance Optimizations Demonstration");
130 println!("========================================\n");
131
132 // 1. Thread Pool Manager
133 println!("1. Thread Pool Manager:");
134 let thread_pool = ThreadPoolManager::new(Some(4))?;
135 println!(
136 " Created thread pool with {} threads",
137 thread_pool.num_threads()
138 );
139
140 // Demonstrate parallel matrix multiplication
141 let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142 let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144 let start_time = std::time::Instant::now();
145 let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146 let elapsed = start_time.elapsed();
147
148 println!(
149 " Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150 matrix_a.shape()[0],
151 matrix_a.shape()[1],
152 matrix_b.shape()[0],
153 matrix_b.shape()[1],
154 result.shape()[0],
155 result.shape()[1]
156 );
157 println!(" Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158 println!(" Result sample: {:.1}", result[[0, 0]]);
159
160 // 2. Performance Profiler
161 println!("\n2. Performance Profiler:");
162 let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164 // Simulate some operations with profiling
165 {
166 let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167 let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168 let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169 optimizer
170 .profiler_mut()
171 .end_timer("matrix_setup".to_string(), timer);
172
173 let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174 }
175
176 println!(" Performance profile summary:");
177 optimizer.profiler().print_summary();
178
179 // 3. Optimization Capabilities
180 println!("\n3. Optimization Capabilities:");
181 let capabilities = optimizer.get_capabilities();
182 println!("{}", capabilities);
183
184 println!("✅ Performance optimizations demonstration completed!\n");
185 Ok(())
186}
Sourcepub fn end_timer(&mut self, name: String, start_time: Option<Instant>)
pub fn end_timer(&mut self, name: String, start_time: Option<Instant>)
End timing an operation and record the result
Examples found in repository?
examples/new_features_showcase.rs (line 171)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129 println!("⚡ Performance Optimizations Demonstration");
130 println!("========================================\n");
131
132 // 1. Thread Pool Manager
133 println!("1. Thread Pool Manager:");
134 let thread_pool = ThreadPoolManager::new(Some(4))?;
135 println!(
136 " Created thread pool with {} threads",
137 thread_pool.num_threads()
138 );
139
140 // Demonstrate parallel matrix multiplication
141 let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142 let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144 let start_time = std::time::Instant::now();
145 let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146 let elapsed = start_time.elapsed();
147
148 println!(
149 " Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150 matrix_a.shape()[0],
151 matrix_a.shape()[1],
152 matrix_b.shape()[0],
153 matrix_b.shape()[1],
154 result.shape()[0],
155 result.shape()[1]
156 );
157 println!(" Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158 println!(" Result sample: {:.1}", result[[0, 0]]);
159
160 // 2. Performance Profiler
161 println!("\n2. Performance Profiler:");
162 let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164 // Simulate some operations with profiling
165 {
166 let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167 let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168 let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169 optimizer
170 .profiler_mut()
171 .end_timer("matrix_setup".to_string(), timer);
172
173 let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174 }
175
176 println!(" Performance profile summary:");
177 optimizer.profiler().print_summary();
178
179 // 3. Optimization Capabilities
180 println!("\n3. Optimization Capabilities:");
181 let capabilities = optimizer.get_capabilities();
182 println!("{}", capabilities);
183
184 println!("✅ Performance optimizations demonstration completed!\n");
185 Ok(())
186}
Sourcepub fn time_operation<F, R>(&mut self, name: &str, operation: F) -> Rwhere
F: FnOnce() -> R,
pub fn time_operation<F, R>(&mut self, name: &str, operation: F) -> Rwhere
F: FnOnce() -> R,
Time a closure and return its result
Sourcepub fn get_timings(&self) -> &HashMap<String, Duration>
pub fn get_timings(&self) -> &HashMap<String, Duration>
Get timing information
Sourcepub fn get_call_counts(&self) -> &HashMap<String, usize>
pub fn get_call_counts(&self) -> &HashMap<String, usize>
Get call counts
Sourcepub fn get_average_time(&self, name: &str) -> Option<Duration>
pub fn get_average_time(&self, name: &str) -> Option<Duration>
Get average timing for an operation
Sourcepub fn print_summary(&self)
pub fn print_summary(&self)
Print timing summary
Examples found in repository?
examples/new_features_showcase.rs (line 177)
128fn demonstrate_performance_optimizations() -> Result<(), Box<dyn std::error::Error>> {
129 println!("⚡ Performance Optimizations Demonstration");
130 println!("========================================\n");
131
132 // 1. Thread Pool Manager
133 println!("1. Thread Pool Manager:");
134 let thread_pool = ThreadPoolManager::new(Some(4))?;
135 println!(
136 " Created thread pool with {} threads",
137 thread_pool.num_threads()
138 );
139
140 // Demonstrate parallel matrix multiplication
141 let matrix_a = Array::from_elem((100, 50), 2.0f32).into_dyn();
142 let matrix_b = Array::from_elem((50, 75), 3.0f32).into_dyn();
143
144 let start_time = std::time::Instant::now();
145 let result = thread_pool.parallel_matmul(&matrix_a, &matrix_b)?;
146 let elapsed = start_time.elapsed();
147
148 println!(
149 " Parallel matrix multiplication: {}x{} * {}x{} = {}x{}",
150 matrix_a.shape()[0],
151 matrix_a.shape()[1],
152 matrix_b.shape()[0],
153 matrix_b.shape()[1],
154 result.shape()[0],
155 result.shape()[1]
156 );
157 println!(" Time elapsed: {:.3}ms", elapsed.as_secs_f64() * 1000.0);
158 println!(" Result sample: {:.1}", result[[0, 0]]);
159
160 // 2. Performance Profiler
161 println!("\n2. Performance Profiler:");
162 let mut optimizer = PerformanceOptimizer::new(Some(1024), Some(512), Some(4), true)?;
163
164 // Simulate some operations with profiling
165 {
166 let timer = optimizer.profiler_mut().start_timer("matrix_setup");
167 let test_matrix_a = Array::from_elem((200, 100), 1.5f32).into_dyn();
168 let test_matrix_b = Array::from_elem((100, 150), 2.5f32).into_dyn();
169 optimizer
170 .profiler_mut()
171 .end_timer("matrix_setup".to_string(), timer);
172
173 let _result = optimizer.optimized_matmul(&test_matrix_a, &test_matrix_b)?;
174 }
175
176 println!(" Performance profile summary:");
177 optimizer.profiler().print_summary();
178
179 // 3. Optimization Capabilities
180 println!("\n3. Optimization Capabilities:");
181 let capabilities = optimizer.get_capabilities();
182 println!("{}", capabilities);
183
184 println!("✅ Performance optimizations demonstration completed!\n");
185 Ok(())
186}
Sourcepub fn get_stats(&self) -> ProfilingStats
pub fn get_stats(&self) -> ProfilingStats
Get profiling statistics
Sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Enable or disable profiling
Auto Trait Implementations§
impl Freeze for PerformanceProfiler
impl RefUnwindSafe for PerformanceProfiler
impl Send for PerformanceProfiler
impl Sync for PerformanceProfiler
impl Unpin for PerformanceProfiler
impl UnwindSafe for PerformanceProfiler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more