Generated banner for Matlab Optimize Performance
Generated logo for Matlab Optimize Performance

Matlab Optimize Performance

MathWorksOfficialCreated by MathWorks

  • Skill
  • Workflow
  • Software development
  • BSD-2-Clause
  • No issues found

From the author

Use when MATLAB code is too slow or the user wants it faster, or asks to profile it, find its hot line or where the time goes, count how many times a function runs, or benchmark or compare timings. Profiles before changing code (Sampling Profiler on R2026b+), checks the output against the original, and reports only measured speedups. Not for writing performance tests.
— the description by MathWorks

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a rigorous, data-driven methodology for identifying and resolving performance bottlenecks in MATLAB. It emphasizes profiling before modification, verifying performance gains through ablation, and ensuring numerical correctness throughout the optimization process. The workflow is designed to move beyond guesswork by using precise timing tools and established optimization patterns, ensuring that reported speedups are genuine and reproducible.

What it does

  • Establishes performance baselines using timeit for accurate measurement.
  • Identifies bottlenecks using the Sampling Profiler or standard MATLAB profiler.
  • Provides a catalog of proven optimization patterns like vectorization and preallocation.
  • Validates performance gains through ablation and interleaved paired timing.
  • Ensures numerical correctness with tolerance-scaled comparisons.
  • Generates structured performance reports for documentation and verification.

When to use it

  • When MATLAB code execution is too slow and requires optimization.
  • When you need to identify specific lines or functions causing performance degradation.
  • When you need to benchmark or compare the performance of different code versions.
  • When you need to verify that an optimization maintains numerical accuracy.

Advantages

  • Reduces reliance on intuition by using empirical profiling data.
  • Prevents false optimizations through the ablation measurement protocol.
  • Ensures code remains correct while becoming faster.
  • Provides a standardized approach for reporting performance improvements.

Usage examples

“My MATLAB script is running slowly. How can I find the bottleneck?”
The agent will guide you through establishing a baseline with timeit and using the Sampling Profiler to identify the specific lines of code consuming the most execution time.
“I think vectorizing this loop will speed up my code. How do I verify it?”
The agent will help you implement the vectorized version, perform an ablation test to confirm the speedup, and run a correctness check to ensure the output matches the original loop.

Install

Documentation by MathWorks

MATLAB Performance Optimization Workflow

Systematic 7-step workflow for finding and fixing performance bottlenecks in MATLAB code.

When to Use

  • User asks to speed up MATLAB code, or to optimize it for execution time

  • User wants to find why their MATLAB code is slow

  • User has a function or script that takes too long to run

  • User asks to benchmark or time MATLAB code in the current session (a one-off timing or an A/B of two versions)

  • User wants to compare performance before and after a change

  • User asks about MATLAB performance best practices

When NOT to Use

  • Optimizing Simulink model simulation speed (use Simulink Profiler)

  • Speeding code up by compiling it with MATLAB Coder (codegen, MEX), or tuning generated MEX (use the matlab-generate-code skill)

  • The problem is memory, not speed: out-of-memory errors or peak memory (use the matlab-optimize-memory skill)

  • The performance issue is purely I/O-bound (file reads, network, database)

  • User wants to write performance tests, or track timings across builds or in CI (use the matlab-write-performance-tests skill)

  • Note: for exact invocation counts, or a sub-10 ms / MEX / parallel workload, use the MATLAB profiler (profile) rather than the Sampling Profiler (Step 2) — that's a profiler choice, not a reason to skip the skill

The 7-Step Workflow

Step 1: Establish Baseline

Measure current performance so you have a number to improve against:

f = @() targetFunction(input1, input2);
baseline = timeit(f);   % handles warm-up and repeats; prefer it to tic/toc

For GPU code use gputimeit. For a script, run it once to warm up, then time a second run with tic/toc. Templates: assets/measurement-templates.md.

Step 2: Profile and Analyze

Find where the time actually goes — don't guess, profile.

Pick the profiler first: check, don't assume. In R2026b+ the Sampling Profiler is the default: a low-overhead statistical profiler that samples the execution thread at ~100 Hz. This skill's sampling_profiler helper drives it, and sampling_profiler('available') tells you whether this MATLAB has it. Use the MATLAB profiler (profile) only when available is false, when you need exact call counts, or for a one-shot call too short to loop, or MEX or parallel code.

% Sampling Profiler — use when sampling_profiler('available') is true (clean up after)
addpath(fullfile(skillRoot, "scripts"));   % skillRoot = directory containing this SKILL.md
assert(sampling_profiler('available'));     % false -> use the MATLAB profiler block below
nReps = 1;   % raise until the loop below runs several seconds (~10 s is ideal)
sampling_profiler('on');
sampling_profiler('pause');
targetFunction(input1, input2);            % warm-up: pays first-call cost, not collected
sampling_profiler('clear');
sampling_profiler('resume');
for r = 1:nReps, targetFunction(input1, input2); end
[~, stacks] = sampling_profiler('off');
sampling_profiler('destroy');              % mandatory cleanup: skipping it corrupts later runs
% MATLAB profiler — any release; use when the Sampling Profiler isn't available
profile on; targetFunction(input1, input2); profile off; profile viewer;

Reading the sampler:

  • Run several seconds; ~10 s is ideal (~1000 samples; ~100 is the bare minimum). Loop a quick workload by raising nReps, and keep the warm-up call. If it can't be looped, use profile.

  • The counts describe the profiled run (stacks.TotalSamples/100 is close to its wall clock) and include no invocation counts. For how many times something ran, use profile, and record "call_count_source": "builtin_profile_numcalls", "sampling_can_count_invocations": false and "sampling_profiler_samples" in perf_report.json.

  • Neither profiler sees inside a builtin or P-coded frame: the time lands on the call site with no line detail. Confirm such a hotspot by removing it and re-timing (Step 5).

Work from the profile to a change:

  • Rank functions by self time, then read the lines inside the top one. Per-line time includes callees, so a line that only makes a call can outrank the line doing the work.

  • Before changing anything, say from the code why that line is slow: a real mechanism (a reallocation in a loop, a builtin on a large array), not just a big percentage.

  • Step 5 is the proof: if re-timing doesn't move the clock, your explanation was wrong; profile again.

For the full Sampling Profiler API, results model, traps, and helpers, see references/sampling-profiler.md.

Step 3: Identify Optimization Opportunities

Based on profiling results, identify which patterns apply. Read references/optimization-patterns.md for the full catalog.

PatternTypical SpeedupLook For
Vectorization2–200xLoops doing element-wise math on arrays
Preallocation2–100xArrays growing inside loops (x = [x; newRow])
Unnecessary recomputation2–50xSame expensive expression computed multiple times
discretize/histcounts2–50xLoops binning or classifying data
Persistent caching1.5–95xRepeated load() or expensive object creation
Logical indexing1.2–5xUsing find() just to index into an array
arguments block1.1–1.8xFunctions using inputParser
Algebraic simplification1.5–3xRedundant sqrt, abs, or matrix ops

Before optimizing, verify the target is worth it:

  • Is it called in a tight loop? High call count × small time = big total.

  • Is it M-code or a built-in? You can't make a built-in faster, but you can often call it fewer times (e.g., pass a matrix to filtfilt/filter instead of looping over columns).

Step 4: Implement Optimizations

Apply the patterns from Step 3, starting with the highest-impact one. Before/after examples are in references/optimization-patterns.md.

  • Move invariant work out of loops (object creation, option parsing, constant expressions)

  • Replace element-wise loops with array operations where possible

  • Use purpose-built functions (discretize, cumsum, hypot) instead of hand-written equivalents

  • For large data, batch the vectorization to control memory (see Pattern 9 in catalog)

  • Try each new variant on a small slice of the input first: a wrong orientation in implicit expansion can build a huge array

Step 5: Measure Optimized Performance

Quick check, while you are still iterating: re-time with timeit as in Step 1 and compare with the baseline. A speedup of 1.2x or more is considered significant; below that, measurement noise makes it hard to be confident the change helped.

Verified result — required whenever the user asks for a number to report, commit, or share, when they gave you a hypothesis about what is slow, or when two profilers disagree. Follow references/measurement-protocol.md in full:

  1. Measure the ceiling before optimizing — delete or hoist the suspected work, re-time end-to-end, and compute what fraction of total time it actually accounted for; profiler attribution has been observed to overstate it by more than 10x. (This removal-and-re-time step is called ablation, and is recorded as adjudication_method: "removal_ablation".)

  2. Correctness gate against the original in the same session, with a tolerance scaled to the output (100*eps(max(abs(original(:))))), plus an independent witness statistic of the output.

  3. Mutation test — perturb the input; the output must change. Catches code that is fast because it stopped doing the work.

  4. Interleaved paired timing, one session, N ≥ 5, median(tb ./ to). Never compare across sessions or in sequential blocks — machine drift lands entirely on one arm.

  5. Write perf_report.json — every field from a variable a run of the current code computed, never a typed-in literal. Run each step in its own tool call and save its results to a .mat as it finishes, so a fix to the report re-writes it from those files instead of re-running the steps.

The headline speedup_median is always end-to-end. A sub-kernel number goes in kernel_speedup. A refuted hypothesis is a result: report it with the honest speedup, including 1.0.

Step 6: Verify Correctness

Every optimization must produce the same results as the original:

original = originalFunction(input1, input2);
fast = optimizedFunction(input1, input2);

% Scale the tolerance to the output — a fixed 1e-10 is wrong in both directions
tol = 100 * eps(max(abs(original(:))));
maxErr = max(abs(original(:) - fast(:)));
fprintf('Max error: %.2e (tol %.2e)\n', maxErr, tol);
assert(isequal(isnan(original), isnan(fast)) && maxErr <= tol, 'Results differ beyond tolerance!');   % max skips NaN

For non-numeric outputs:

assert(isequal(original, fast), 'Results differ!');

Error at or below eps(max(abs(original(:)))) is floating-point reordering noise — acceptable; document it. Error orders of magnitude above it is a bug, however clean the algebra looked. Watch for catastrophic cancellation: an algebraically exact shortcut (e.g. the running-sums variance formula) can be dramatically faster and still wrong on data with a large mean offset. Subtract the mean first, then check the result against the tolerance again.

An error of exactly 0 proves nothing on its own. Also record an independent statistic of the optimized output (min(fast(:)), a checksum) — see references/measurement-protocol.md.

Step 7: Report Results

Report the baseline, the optimized time, the speedup (for each change and end to end), the max error, and the pattern applied. If you followed the verified-result protocol in Step 5, perf_report.json is the deliverable and this is its human-readable summary.

Key Rules

  1. Always profile before optimizing — never guess where the bottleneck is

  2. Measure the ceiling before you optimize — remove or hoist the suspect and re-time end-to-end; that difference, not the profiler's % column, is the ceiling on what a rewrite can win (this step is ablation)

  3. One change at a time — measure after each optimization to know what helped

  4. Verify correctness — every optimization must produce equivalent output, with a tolerance scaled to the output

  5. Report end-to-end — the headline number is the whole workload; a sub-kernel speedup is reported separately and labelled as such

  6. Every reported number comes from a variable — computed by a run of the current code, never typed in from memory

  7. 1.2x threshold — speedups below 1.2x are not reliably distinguishable from noise

  8. GPU timing — always wait(gpuDevice) before and after timing GPU code

  9. Use timeit — it handles warmup and averaging; avoid raw tic/toc for benchmarks

  10. On R2026a, clear what you profiled before you time anything — code run under profile on R2026a stays ~100x slower for the rest of the session until you clear it, so a baseline taken after a profiled run flatters every "after" number. R2026a also has no Sampling Profiler.

Common Mistakes

MistakeWhy It's WrongDo This Instead
Growing arrays in loopsEach append copies the entire arrayPreallocate before the loop
Vectorizing huge arrays blindlyMay exceed memoryUse chunked processing for large data
Assuming faster = correctBugs can make code fast (by skipping work)Verify results match (Step 6) + mutation test
Fixed assert(maxErr < 1e-10)Rejects clean results on large-magnitude data; passes broken ones on smallScale tolerance to the output: 100*eps(max(abs(original(:))))
Quoting a sub-kernel speedup as the headline8x on 20% of runtime is ~1.2x end-to-endHeadline is end-to-end; kernel number reported separately
Manufacturing a win when the hypothesis was wrongA refuted hypothesis is a valid resultReport speedup_median honestly, including 1.0

Reference Files

  • references/measurement-protocol.md — the verified-result protocol for Step 5 and every perf_report.json field

  • references/sampling-profiler.md — Sampling Profiler (R2026b+): commands, results model, traps, and the scripts/ helper index

  • references/optimization-patterns.md — the pattern catalog, with before/after code and measured speedups

  • assets/measurement-templates.md — templates for a timeit baseline, GPU timing, and a scaling test

  • scripts/ — MATLAB helpers for the Sampling Profiler; addpath this folder to use them

Copyright 2026 The MathWorks, Inc.

Files

scripts/profile_function.m

function results = profile_function(benchFn, opts)
%PROFILE_FUNCTION Profile any function handle with the Sampling Profiler.
%
%   results = profile_function(@() myCode(args))
%   results = profile_function(@myBenchmark, 'Tag', 'mytest')
%   results = profile_function(@() sim(mdl), 'NumBaseline', 5)
%   results = profile_function(@myFn, 'MinTime', 2.0)
%
% For quick profiling of arbitrary code without test framework overhead.
%
% NOTE: Cannot profile scripts directly via @() run('script.m') due to
% MATLAB static workspace restrictions. Wrap script logic in a local
% function instead:
%   profile_function(@myWrapper, 'Tag', 'test')
%   function myWrapper(), ... end
%
% Protocol:
%   1. JIT warmup (one call, no profiler)
%   2. Baseline timing (median of N calls, no profiler)
%   3. Warm-start profiled run (auto-loops if MinTime > baseline)
%
% Use MinTime for short benchmarks (<0.2s) to accumulate enough samples.

arguments
    benchFn (1,1) function_handle
    opts.Tag (1,:) char = 'profile'
    opts.ResultsDir (1,:) char = fullfile(pwd, 'sp_results')
    opts.NumBaseline (1,1) double {mustBePositive, mustBeInteger} = 3
    opts.TopN (1,1) double {mustBePositive, mustBeInteger} = 30
    opts.MinTime (1,1) double {mustBeNonnegative} = 0
    opts.Save (1,1) logical = true
end

assertSamplingProfiler();   % clear error + fallback hint on pre-R2026b

fprintf('=== SP Profile: %s ===\n', opts.Tag);

% --- 1. JIT warmup ---
fprintf('[1/3] JIT warmup...\n');
tid = tic;
benchFn();
fprintf('  Warmup: %.2fs\n', toc(tid));

% --- 2. Baseline ---
fprintf('[2/3] Baseline (%d runs)...\n', opts.NumBaseline);
baseTimes = zeros(1, opts.NumBaseline);
for r = 1:opts.NumBaseline
    tid = tic;
    benchFn();
    baseTimes(r) = toc(tid);
end
baselineTime = median(baseTimes);
fprintf('  Baseline (median): %.3fs\n', baselineTime);

if baselineTime < 0.2
    warning('profile_function:short', ...
        'Benchmark is %.3fs (<0.2s). Results will have high statistical noise.', baselineTime);
end

% --- 3. Profiled run (warm-start) ---
% Steps 1-2 warmed the workload with the profiler OFF; one more warm-up under
% 'pause' after 'on' keeps the profiler's own first-call cost out of the samples.
fprintf('[3/3] Profiled run...\n');
sampling_profiler('on');
% Guard: if benchFn() errors mid-run, always tear the profiler down so a later
% run isn't corrupted by a still-armed session.
spCleanup = onCleanup(@() safeDestroy());
sampling_profiler('pause');
benchFn();                                    % warm-up (not collected)
sampling_profiler('clear');
sampling_profiler('resume');

% If benchmark is too short, loop it
nReps = 1;
if opts.MinTime > 0 && baselineTime < opts.MinTime
    nReps = ceil(opts.MinTime / baselineTime);
    fprintf('  Looping %dx to reach %.1fs minimum...\n', nReps, opts.MinTime);
end

tid = tic;
for rep = 1:nReps
    benchFn();
end
profileTime = toc(tid);

[~, stacks] = sampling_profiler('off');
clear spCleanup;                              % onCleanup -> safeDestroy() tears down now

% --- Report ---
overhead = (profileTime / (baselineTime * nReps) - 1) * 100;
fprintf('\n--- Results: %s ---\n', opts.Tag);
fprintf('  Baseline:      %.3fs\n', baselineTime);
fprintf('  Profiled:      %.3fs (%d reps, overhead: %+.1f%%)\n', profileTime, nReps, overhead);
fprintf('  TotalSamples:  %d\n', stacks.TotalSamples);
fprintf('  Snapshots:     %d\n', numel(stacks.Snapshots));
fprintf('  Implied time:  %.2fs\n', stacks.TotalSamples / 100);

fprintf('\n--- Top %d Hotspots ---\n', opts.TopN);
print_hotspot_report(stacks, opts.TopN);

% --- Save ---
if opts.Save
    if ~isfolder(opts.ResultsDir), mkdir(opts.ResultsDir); end
    resultFile = fullfile(opts.ResultsDir, [opts.Tag '.mat']);
    tag = opts.Tag;
    save(resultFile, 'stacks', 'baselineTime', 'profileTime', ...
        'baseTimes', 'nReps', 'tag');
    fprintf('\nSaved: %s\n', resultFile);
end

% --- Return ---
results.stacks = stacks;
results.baselineTime = baselineTime;
results.profileTime = profileTime;
results.tag = opts.Tag;
if opts.Save
    results.resultFile = resultFile;
end
end

function safeDestroy()
% Tear the profiler down no matter what state it is in (used by the onCleanup
% guard so an error mid-run never leaves a session armed).
    try
        st = sampling_profiler('status');
        if ~strcmp(st, 'off'), sampling_profiler('off'); end
    catch
    end
    try sampling_profiler('destroy'); catch, end
end

% Copyright 2026 The MathWorks, Inc.

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
MathWorks
Publisher
MathWorks (Official)
Source
matlab/matlab-agentic-toolkit · skills-catalog/matlab-software-development/matlab-optimize-performance · commit 2a7f215
Licence
BSD 2-Clause License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE.md)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by MathWorks or MathWorks.

Get it from the source

1 of this skill’s files are not mirrored here, so there is no ZIP. Get the complete skill from its source: matlab/matlab-agentic-toolkit