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/annotate_source_lines.m
View on GitHubfunction annotate_source_lines(stacks)
%ANNOTATE_SOURCE_LINES Print source files with per-line sample counts from profiler stacks.
if isempty(stacks.Snapshots)
fprintf('No samples collected (no snapshots to annotate).\n');
return;
end
% Count samples per file:line (leaf frame = self time). The leaf is Frames(Depth),
% not Frames(end) — Frames may carry entries beyond the reported Depth.
lineMap = containers.Map('KeyType', 'char', 'ValueType', 'int32');
fileSet = containers.Map('KeyType', 'char', 'ValueType', 'logical');
for s = 1:numel(stacks.Snapshots)
snap = stacks.Snapshots(s);
d = min(double(snap.Depth), numel(snap.Frames));
if d > 0
fr = snap.Frames(d);
key = sprintf('%s:%d', fr.FileName, fr.StartLine);
if lineMap.isKey(key)
lineMap(key) = lineMap(key) + 1;
else
lineMap(key) = 1;
end
fileSet(fr.FileName) = true;
end
end
% Percentages are of captured snapshots (numel(Snapshots)), NOT TotalSamples.
% TotalSamples includes idle timer ticks (wall-clock time with no MATLAB frame on
% the stack); dividing by it would understate every line and disagree with
% find_hotspots / print_hotspot_report, which use the snapshot count.
totalSnapshots = numel(stacks.Snapshots);
files = fileSet.keys;
% Sort files by total samples descending
fileSamples = zeros(1, numel(files));
for f = 1:numel(files)
fileSamples(f) = 0;
allKeys = lineMap.keys;
for k = 1:numel(allKeys)
if startsWith(allKeys{k}, [files{f} ':'])
fileSamples(f) = fileSamples(f) + lineMap(allKeys{k});
end
end
end
[fileSamples, sortIdx] = sort(fileSamples, 'descend');
files = files(sortIdx);
for f = 1:numel(files)
fname = files{f};
if fileSamples(f) == 0
continue;
end
% Build line->count map for this file
lineCounts = containers.Map('KeyType', 'int32', 'ValueType', 'int32');
allKeys = lineMap.keys;
for k = 1:numel(allKeys)
if startsWith(allKeys{k}, [fname ':'])
parts = strsplit(allKeys{k}, ':');
lineNum = str2double(parts{end});
lineCounts(lineNum) = lineMap(allKeys{k});
end
end
% Read source file
[~, shortName, ext] = fileparts(fname);
fprintf('\n========== %s%s (%d samples, %.1f%% of total) ==========\n', ...
shortName, ext, fileSamples(f), fileSamples(f)/totalSnapshots*100);
if strcmpi(ext, '.p') || startsWith(ext, '.mex', 'IgnoreCase', true)
fprintf(' (P-coded or MEX; source not available)\n');
continue;
end
try
fid = fopen(fname, 'r');
if fid == -1
fprintf(' (could not read file)\n');
continue;
end
src = {};
while true
line = fgetl(fid);
if ~ischar(line), break; end
src{end+1} = line; %#ok<AGROW>
end
fclose(fid);
% Print sampled lines with 2 lines of context; elide the rest so a big file stays short
show = false(1, numel(src));
hotLines = double(cell2mat(lineCounts.keys));
for h = hotLines(hotLines >= 1 & hotLines <= numel(src))
show(max(1, h-2):min(numel(src), h+2)) = true;
end
for lineNum = 1:numel(src)
if ~show(lineNum)
if lineNum == 1 || show(lineNum-1), fprintf(' | ... |\n'); end
continue;
end
if lineCounts.isKey(lineNum)
cnt = lineCounts(lineNum);
pct = double(cnt) / totalSnapshots * 100; % cnt is int32: divide in double
fprintf('%5d %5.1f%% | %4d | %s\n', cnt, pct, lineNum, src{lineNum});
else
fprintf(' | %4d | %s\n', lineNum, src{lineNum});
end
end
catch
fprintf(' (error reading file)\n');
end
end
fprintf('\n');
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 |