matlab-performance-optimizer

Optimize MATLAB code for better performance through vectorization, memory management, and profiling. Use when user requests optimization, mentions slow code, performance issues, speed improvements, or asks to make code faster or more efficient.

By matlab · 427 installs

npx skills add matlab/agent-skills-playground --skill matlab-performance-optimizer

Source repository · Upstream listing

MATLAB Performance Optimizer Optimize MATLAB code performance with vectorization, memory management, and profiling tools. When to Use This Skill Optimizing slow or inefficient MATLAB code Converting loops to vectorized operations Reducing memory usage Improving algorithm performance When user mentions: slow, performance, optimize, speed up, efficient, memory Profiling code to find bottlenecks Parallelizing computations Core Optimization Principles 1. Vectorization (Most Important) Replace loops with vectorized operations whenever possible. SLOW Using loops: FAST Vectorized: 2. Preallocate Arrays Always preallocate arrays before loops. SLOW Growing arrays: FAST Preallocated: 3. Use Built in Functions MATLAB built in functions are highly optimized. SLOW Manual implementation: FAST Built in function: Vectorization Techniques Element wise Operations Use . , ./ , .^ for element wise operations: Logical Indexing Replace conditional loops with logical indexing: Matrix Operations Use matrix multiplication instead of nested loops: Cumulative Operations Use cumsum , cumprod , cummax , cummin : Memory Optimization Use Appropriate Data Types Sparse Matrices For matrices with mostly zeros: Clear Unused Variables In Place Operations Profiling and Benchmarking Using the Profiler The profiler shows: Time spent in each function Number of calls to each function Lines that take the most time Timing Comparisons Common Optimization Patterns Pattern 1: Replace find with Logical Indexing Pattern 2: Use Implicit Expansion Instead of repmat Pattern 3: Avoid Repeated Calculations Pattern 4: Efficient String Operations Pattern 5: Use Table for Mixed Data Types Algorithm Specific Optimizations Convolution and Filtering Distance Calculations Sorting and Searching Parallel Computing Simple Parallel Loops (parfor) Requirements for parfor: Iterations must be independent Loop variable must be consecutive integers Variables must be classified as loop, sliced, broadcast, or reduction Parallel Array Operations Advanced Optimizations MEX Functions for Critical Sections Convert performance critical code to C/C++: Persistent Variables for Cached Results JIT Acceleration Best Practices MATLAB's JIT (Just In Time) compiler optimizes: Simple for loops with scalar operations Functions without dynamic features JIT friendly code: JIT unfriendly code (avoid): Performance Checklist Before finalizing optimized code, verify: [ ] Loops are vectorized where possible [ ] Arrays are preallocated before loops [ ] Built in functions used instead of manual implementations [ ] Logical indexing used instead of find + indexing [ ] Appropriate data types used (single vs double, integers) [ ] Sparse matrices used for sparse data [ ] Repeated calculations moved outside loops [ ] String concatenation uses efficient methods [ ] Code profiled to identify actual bottlenecks [ ] Matrix operations used instead of element wise loops [ ] Parallel computing considered for independent operations [ ] Memory intensive operations optimized [ ] Caching implemented for repeated expensive calls Profiling Workflow 1. Measure First : Profile before optimizing 2. Identify Bottlenecks : Focus on functions taking most time 3. Optimize : Apply appropriate techniques 4. Measure Again : Verify improvement 5. Iterate : Repeat for remaining bottlenecks Common Performance Pitfalls Pitfall 1: Premature Optimization Profile first, optimize second Focus on actual bottlenecks, not assumptions Pitfall 2: Over vectorization Sometimes loops are clearer and fast enough Balance readability with performance Pitfall 3: Ignoring Memory Access Patterns Pitfall 4: Unnecessary Data Type Conversions Optimization Examples Example 1: Image Processing Example 2: Statistical Analysis Example 3: Time Series Processing Troubleshooting Performance Issue : Code still slow after vectorization Solution : Profile to find new bottlenecks; consider algorithm complexity Issue : Out of memory errors Solution : Use smaller data types, process in chunks, use sparse matrices Issue : parfor slower than for loop Solution : Check if overhead outweighs benefits; ensure iterations are expensive enough Issue : GPU computation slower than CPU Solution : Data transfer overhead may exceed computation time; use for large arrays Additional Resources Use profile viewer to analyze performance Use memory to check memory usage Use doc with: timeit , tic/toc , parfor , gpuArray , sparse Check MATLAB Performance and Memory documentation