matlab/matlab-agentic-toolkit / skills-catalog/matlab-software-development/matlab-optimize-performance
matlab-optimize-performance
UnverifiedUse 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.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 13.0 KB |
| assets/measurement-templates.md | markdown | 1.9 KB |
| manifest.yaml | config | 554 B |
| references/measurement-protocol.md | markdown | 14.7 KB |
| references/optimization-patterns.md | markdown | 7.4 KB |
| references/sampling-profiler.md | markdown | 11.1 KB |
| scripts/annotate_source_lines.m | other | 3.9 KB |
| scripts/assertSamplingProfiler.m | other | 913 B |
| scripts/compare_profiles.m | other | 9.0 KB |
| scripts/filter_test_frames.m | other | 2.3 KB |
| scripts/find_hotspots.m | other | 2.5 KB |
| scripts/make_ab_commands.m | other | 4.2 KB |
| scripts/print_hotspot_report.m | other | 3.9 KB |
| scripts/profile_function.m | other | 4.4 KB |
| scripts/profile_script.m | other | 2.7 KB |
| scripts/profile_test.m | other | 5.4 KB |
| scripts/quick_profile.m | other | 2.6 KB |
| scripts/sampling_profiler.p | binary | 587 B |
scripts/profile_script.m
View on GitHubfunction [stacks, hot] = profile_script(scriptPath, topN)
%PROFILE_SCRIPT Profile a MATLAB SCRIPT file (not a function) and print hotspots.
%
% profile_script('myAnalysis.m')
% profile_script('myAnalysis.m', 20)
% [stacks, hot] = profile_script(fullfile(myDir,'myAnalysis.m'))
%
% WHY THIS EXISTS: you cannot profile a script via @()run('script.m') — MATLAB
% rejects it with "Attempt to add variable to static workspace". This helper
% runs the script through `run` inside a normal (non-anonymous) function frame,
% which is allowed, and captures the profile around it.
%
% The script runs in a workspace of its own, so a script that starts with `clear`
% cannot break the capture. Its variables do not leak back to the caller.
%
% INPUT
% scriptPath : path to a .m script file (a file with no `function` line, or a
% script you normally invoke by name).
% topN : hotspots to print (default 15).
%
% OUTPUT (optional)
% stacks : raw stacks struct; hot : top-N hotspots as data (see find_hotspots).
%
% For a FUNCTION handle use quick_profile / profile_function instead.
if nargin < 2 || isempty(topN), topN = 15; end
assertSamplingProfiler(); % clear error + fallback hint on pre-R2026b
assert(isfile(scriptPath), 'profile_script:notFound', 'Script not found: %s', scriptPath);
% Warm-start protocol: start -> PAUSE -> warm-up run (NOT collected) ->
% clear -> resume -> measured run. Warming up after 'on' rather than before
% it leaves less first-call cost in the measured run.
sampling_profiler('on');
cleanup = onCleanup(@() safeDestroy());
sampling_profiler('pause');
runIsolated(scriptPath); % warm-up (not collected)
sampling_profiler('clear');
sampling_profiler('resume');
runIsolated(scriptPath); % measured run
[~, stacks] = sampling_profiler('off');
clear cleanup; % onCleanup -> safeDestroy() tears down now
fprintf('profile_script(%s): %d samples (%.2fs implied)\n', ...
scriptPath, stacks.TotalSamples, stacks.TotalSamples/100);
if stacks.TotalSamples < 20
warning('profile_script:fewSamples', ...
'Only %d samples — statistically noisy.', stacks.TotalSamples);
end
print_hotspot_report(stacks, topN);
if nargout > 1, hot = find_hotspots(stacks, topN); end
if nargout == 0, clear stacks; end % called for the printed report only: don't echo the struct
end
function runIsolated(scriptPath)
run(scriptPath);
end
function safeDestroy()
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.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |