[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M73556D91SJRN5JDMFVRWSAJ":3,"skill-same:01M73556D91SJRN5JDMFVRWSAJ":53},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":32,"commitSha":33,"skillDirectory":34,"forksCount":35,"repositoryHtmlUrl":36,"repositoryHomepageUrl":37,"ownerHtmlUrl":38,"ownerBlogUrl":6,"ownerDescription":6,"updatedAt":39,"install":40,"resources":52},"01M73556D91SJRN5JDMFVRWSAJ","measure-performance",null,"Profile and benchmark.","Morrison-Lab\u002Fai-config","Morrison-Lab","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F104235520?v=4","01M734G0451S2G35MN4HFQK7XP","Morrison Lab","01M734G059BYFDFQA8YTPDGPMH",4,"MIT","https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fmeasure-performance\u002FSKILL.md",[],[],"2026-11-11 15:11:14",false,"---\nname: measure-performance\ndescription: \"Profile and benchmark.\"\nuser-invocable: true\nallowed-tools:\n  - Bash\n  - Read\n  - Edit\n  - Write\n  - Grep\n  - Glob\n---\n\n# measure-performance -- profile first, then microbenchmark (perf, benchmark)\n\nFind out what is actually slow before changing anything, then prove the change\nhelped.\nThe method is [Advanced R, ch. 23 \"Measuring\nperformance\"](https:\u002F\u002Fadv-r.hadley.nz\u002Fperf-measure.html):\n[profvis](https:\u002F\u002Frstudio.github.io\u002Fprofvis\u002F) to locate the bottleneck,\n[bench](https:\u002F\u002Fbench.r-lib.org\u002F) to compare alternatives for it.\n\n**Why this exists.**\nIntuition about what is slow is unreliable, even for experienced programmers,\nso the chapter opens with Knuth's warning that time spent worrying about the\nspeed of noncritical code has a strong negative effect on debugging and\nmaintenance.\nTwo failure modes follow from skipping measurement.\nOptimizing the wrong line makes code harder to read and no faster.\nWinning a microbenchmark and reporting it as a speedup makes a claim the real\nworkload does not support: a 2x win on an operation that takes 2 microseconds\nof a 30-second job is not a win.\nThis skill is [`algorithmatize-checks`](..\u002F..\u002Fshared\u002Fworkflow\u002Falgorithmatize-checks.md)\napplied to performance claims, and it operationalizes the \"performance tuning\nbeyond that needs a demonstrated hot spot, not speculation\" clause of the\nEfficient goal in [the principles catalog](..\u002F..\u002Fshared\u002Fprinciples\u002FREADME.md).\n\n## When this fires\n\n- \"measure performance\", \"perf\", \"benchmark\", \"benchmark this\",\n  \"microbenchmark\", \"profile this\", \"run profvis\", \"use `bench::mark()`\"\n- \"why is this slow\", \"what is the bottleneck\", \"find the hot spot\"\n- \"which version is faster\", \"is this actually faster\", \"time these two\n  approaches\", \"how much did that speed things up\"\n- Reviewing or writing a diff that asserts a speedup without numbers, or that\n  proposes a micro-optimization with no profile behind it.\n\n## Procedure\n\n### 1. Check the measurement is worth taking\n\nAnswer these before installing anything:\n\n- **Is something actually slow?**\n  Name the workload and roughly how long it takes now.\n  A script nobody waits on does not need a profile.\n- **Is it slow on a realistic input?**\n  A profile of a toy input measures the toy.\n- **What would \"fast enough\" be?**\n  Without a target there is no way to stop, and the loop runs until the code\n  is unreadable.\n\nIf the answer to the first question is \"no\", say so and stop.\nThat is a complete, correct result for this skill, not a failure to deliver.\n\n### 2. Fix the input and a correctness baseline\n\nPut the code under measurement in its own file and `source()` it, so the\nprofiler can link its samples back to source lines.\n\nRecord what the current code returns on the realistic input.\nEvery alternative gets checked against it later:\n\n```r\nsource(\"\u003Cscript>.R\")\nbaseline \u003C- \u003Ccall-under-test>\nsaveRDS(baseline, \"baseline.rds\")\n```\n\nSpeed is only interesting among alternatives that agree.\n`bench::mark()` enforces that in step 5, but a saved baseline also catches an\nalternative that changes results in a way the benchmark never evaluates.\n\n### 3. Profile: locate the bottleneck, do not guess\n\nR's profiler is a sampling profiler: it stops execution every few\nmilliseconds and records the call stack.\nThat keeps overhead low at the cost of being stochastic, so successive\nprofiles differ slightly.\nThe variability mostly affects functions that take very little time, which\nare the ones you care least about.\n\nThe interactive route, which links the profile back to source lines:\n\n```r\nsource(\"\u003Cscript>.R\")\np \u003C- profvis::profvis(\u003Ccall-under-test>)\n```\n\nIf the call finishes before any sample lands, profvis aborts with\n`No parsing data available. Maybe your function was too fast?`.\nThat is the profiler telling you the input is not realistic enough to profile,\nso go back to step 1 rather than shrinking the sampling interval.\n\nIn a headless session, save the widget and open it later:\n\n```r\nhtmlwidgets::saveWidget(p, \"profile.html\", selfcontained = TRUE)\n```\n\n`selfcontained = TRUE` needs pandoc, and htmlwidgets discovers it through\n`{rmarkdown}`: as of htmlwidgets 1.6.0 that path \"now uses the `{rmarkdown}`\npackage to discover and call pandoc\"\n([NEWS](https:\u002F\u002Fgithub.com\u002Framnathv\u002Fhtmlwidgets\u002Fblob\u002Fmaster\u002FNEWS.md)).\n`rmarkdown::pandoc_available()` is therefore the gate that matches what\nhtmlwidgets itself consults, rather than a proxy for it.\nWithout pandoc the call aborts rather than degrading quietly, with\n`Saving a widget with selfcontained = TRUE requires pandoc.` (verified on\nhtmlwidgets 1.6.4).\nDrop the argument to `FALSE` in that case, which writes a sidecar\ndependencies directory next to the HTML instead.\n\nWhen there is no way to view HTML at all, take the text summary instead:\n\n```r\nsource(\"\u003Cscript>.R\")\ntmp \u003C- tempfile()\nRprof(tmp, interval = 0.01, memory.profiling = TRUE)\n\u003Ccall-under-test>\nRprof(NULL)\nsummaryRprof(tmp, memory = \"both\")$by.self\n```\n\n`by.self` ranks functions by time spent in the function itself rather than in\nits callees, which is what points at the line to change.\n\nUse `profvis::pause()`, never `Sys.sleep()`, when building a synthetic\nexample to reason about: as far as R can tell, `Sys.sleep()` uses no\ncomputing time, so it never appears in the profile.\n\n### 4. Read the profile\n\nThe flame graph shows the full call stack, so a function called from two\nplaces is visible as two stacks rather than one aggregate.\nWatch for two things in particular.\n\n**A function high in the self-time ranking because it is called often, not\nbecause it is slow.**\nThe fix is the call count, not the function.\n\n**`\u003CGC>`.**\nThis entry is the garbage collector, not your code.\nA lot of time in `\u003CGC>` almost always means many short-lived objects, and the\nusual cause is copy-on-modify in a loop that grows an object one element at a\ntime:\n\n```r\nx \u003C- integer()\nfor (i in 1:1e4) x \u003C- c(x, i)     # every iteration copies all of x\n```\n\nConfirm it from the memory column: a line that allocates and frees large\namounts on every pass is the one to fix, and the fix is preallocation, not a\nfaster arithmetic operator.\n\n### 5. Microbenchmark only the bottleneck\n\n`bench::mark()` uses a high-precision timer, so it can separate operations\nthat take microseconds.\nBenchmark the one expression the profile implicated, with the realistic input\nfrom step 2, not the whole pipeline:\n\n```r\nx \u003C- runif(100)\nlb \u003C- bench::mark(\n  sqrt(x),\n  x^0.5\n)\nlb[c(\"expression\", \"min\", \"median\", \"itr\u002Fsec\", \"n_gc\", \"mem_alloc\")]\n```\n\nBy default each expression runs at least once (`min_iterations = 1`) and then\nas many times as fit in half a second (`min_time = 0.5`).\n\n`bench::mark()` checks that every expression returns the same value and\naborts if they differ:\n\n```text\nError : Each result must equal the first result:\n`sum(x)` does not equal `mean(x)`\n```\n\nThat error is usually the benchmark catching a real bug in the alternative.\nSet `check = FALSE` only when the expressions are *meant* to return different\nthings, and say in the report why.\n\nUse `bench::press()` when the answer depends on input size, so the comparison\nruns across a grid of sizes rather than at one arbitrary point.\n\n### 6. Interpret the numbers\n\n**Read `min` and `median`, not the mean.**\nThe timing distribution is heavily right-skewed, and often multimodal because\nthe machine is doing other things.\n`min` is the best achievable time and `median` the typical one;\n`plot(lb)` shows the distribution on a log x-axis.\nThe returned tibble carries `expression`, `min`, `median`, `itr\u002Fsec`,\n`mem_alloc`, `gc\u002Fsec`, `n_itr`, `n_gc`, `total_time`, and the list-columns\n`result`, `memory`, `time`, and `gc` (verified against bench 1.1.4).\n\n**Report absolute units, not just a ratio.**\n\"2.1x faster\" is not actionable on its own.\nCalibrate with how many calls it takes to reach one second:\n\n| Per call | Calls per second of run time |\n|---|---|\n| 1 ms | one thousand |\n| 1 us | one million |\n| 1 ns | one billion |\n\nSo an expression at 1.4 us that the real workload calls a few hundred times\ncannot account for a slow job, however large its ratio, and swapping it is\nchurn.\n\n**Check `mem_alloc` and `n_gc` alongside the times.**\nWhen step 4 flagged `\u003CGC>`, these are the columns that show whether the\nalternative actually allocates less.\n\n### 7. Re-profile at real scale\n\nA microbenchmark measures a snippet in isolation.\nReal code is dominated by higher-order effects, so a microbenchmark win is a\nhypothesis about the real workload, not a result about it.\nApply the change, re-run step 3 on the full realistic workload, and compare\nagainst the original wall-clock time from step 1.\n\nIf the end-to-end time did not move, revert the change.\nA faster expression that leaves the job the same length has bought nothing\nand cost readability.\n\n### 8. Report\n\nState, in this order:\n\n1. The workload, its input, and its before\u002Fafter wall-clock time.\n2. The bottleneck the profile identified, and how much of total time it held.\n3. The `bench::mark()` table for the alternatives, with units.\n4. Whether the end-to-end re-profile confirmed the win.\n5. Which limitations below apply, if any.\n\nAttach the numbers to the PR when the change ships, so the next reviewer does\nnot have to re-derive them.\n\n## Limitations to state in the report\n\nThe profiler cannot see everything, and each gap silently misattributes time:\n\n- **Compiled code.**\n  Profiling shows that R called into C\u002FC++ but not what happened inside.\n  Say so rather than reporting the C call as a leaf.\n- **Anonymous functions.**\n  Heavy functional-programming code is hard to attribute call by call.\n  Name the functions before profiling.\n- **Lazy evaluation.**\n  An argument is evaluated where it is first used, so in `j(i())` the profile\n  attributes `i()`'s cost to `j()`.\n  Use `force()` to pull evaluation forward when the attribution matters.\n\n## Outside R\n\nThe tools are R-specific; the order is not.\nProfile before optimizing, microbenchmark only the bottleneck the profile\nfound, read medians rather than means, and confirm the win end to end.\nIn Python the stdlib equivalents of the two measurement steps are `cProfile`\nand `timeit`.\n\n## Relationship to other skills\n\n- [`algorithmatize-checks`](..\u002F..\u002Fshared\u002Fworkflow\u002Falgorithmatize-checks.md) --\n  the general rule this skill instruments: a performance claim is decidable by\n  measurement, so never settle it by reasoning.\n- [`dont-reinvent-wheel`](..\u002F..\u002Fshared\u002Fprinciples\u002Fdont-reinvent-wheel.md) and\n  [`prefer-packaged-functions`](..\u002F..\u002Fshared\u002Fcoding\u002Fprefer-packaged-functions.md)\n  -- check for an existing, usually C-backed, packaged implementation before\n  hand-optimizing.\n  A found package beats a won benchmark.\n- [`use-memoisation`](..\u002F..\u002Fshared\u002Fcoding\u002Fuse-memoisation.md) -- one of the\n  fixes this skill's profile can point at, and the fragment that says to raise\n  a missed memoisation only when the function is \"demonstrably hot\".\n  Step 3 is what demonstrates it, and step 6's `mem_alloc` column is where the\n  memory half of the trade shows up.\n- [`reprexes`](..\u002Freprexes\u002FSKILL.md) -- reduces a slow workload to the\n  minimal self-contained snippet step 5 benchmarks.\n- [`test`](..\u002Ftest\u002FSKILL.md) and [`r-pkg-check`](..\u002Fr-pkg-check\u002FSKILL.md) --\n  the correctness gates an optimization still has to pass.\n  `bench::mark()`'s equality check compares the benchmarked expressions to\n  each other, not the package to its test suite.\n- [`simplify`](..\u002Fsimplify\u002FSKILL.md) and [`tidy`](..\u002Ftidy\u002FSKILL.md) -- an\n  optimization step 7 could not confirm is complexity debt; hand the cleanup\n  to these.\n- [`ardi`](..\u002Fardi\u002FSKILL.md) -- the loop that carries step 8's numbers into a\n  review round, whether the finding is yours or a reviewer's.\n\n## Anti-patterns\n\n- Optimizing a line because it looks slow, with no profile.\n- Profiling a toy input, then acting on the result as if it described the\n  real workload.\n- Microbenchmarking the whole pipeline instead of the bottleneck the profile\n  named.\n- Comparing means, or reporting a ratio with no absolute units behind it.\n- Passing `check = FALSE` to silence a genuine difference in results, rather\n  than because the expressions are meant to differ.\n- Shipping a microbenchmark win without the end-to-end re-profile of step 7.\n- Reading `\u003CGC>` as slow code rather than as an allocation problem.\n- Using `Sys.sleep()` in a synthetic profiling example, where it is invisible\n  to the profiler.\n- Asserting a speedup in a PR body with no numbers attached.\n","# measure-performance -- profile first, then microbenchmark (perf, benchmark)\n\nFind out what is actually slow before changing anything, then prove the change\nhelped.\nThe method is [Advanced R, ch. 23 \"Measuring\nperformance\"](https:\u002F\u002Fadv-r.hadley.nz\u002Fperf-measure.html):\n[profvis](https:\u002F\u002Frstudio.github.io\u002Fprofvis\u002F) to locate the bottleneck,\n[bench](https:\u002F\u002Fbench.r-lib.org\u002F) to compare alternatives for it.\n\n**Why this exists.**\nIntuition about what is slow is unreliable, even for experienced programmers,\nso the chapter opens with Knuth's warning that time spent worrying about the\nspeed of noncritical code has a strong negative effect on debugging and\nmaintenance.\nTwo failure modes follow from skipping measurement.\nOptimizing the wrong line makes code harder to read and no faster.\nWinning a microbenchmark and reporting it as a speedup makes a claim the real\nworkload does not support: a 2x win on an operation that takes 2 microseconds\nof a 30-second job is not a win.\nThis skill is [`algorithmatize-checks`](..\u002F..\u002Fshared\u002Fworkflow\u002Falgorithmatize-checks.md)\napplied to performance claims, and it operationalizes the \"performance tuning\nbeyond that needs a demonstrated hot spot, not speculation\" clause of the\nEfficient goal in [the principles catalog](..\u002F..\u002Fshared\u002Fprinciples\u002FREADME.md).\n\n## When this fires\n\n- \"measure performance\", \"perf\", \"benchmark\", \"benchmark this\",\n  \"microbenchmark\", \"profile this\", \"run profvis\", \"use `bench::mark()`\"\n- \"why is this slow\", \"what is the bottleneck\", \"find the hot spot\"\n- \"which version is faster\", \"is this actually faster\", \"time these two\n  approaches\", \"how much did that speed things up\"\n- Reviewing or writing a diff that asserts a speedup without numbers, or that\n  proposes a micro-optimization with no profile behind it.\n\n## Procedure\n\n### 1. Check the measurement is worth taking\n\nAnswer these before installing anything:\n\n- **Is something actually slow?**\n  Name the workload and roughly how long it takes now.\n  A script nobody waits on does not need a profile.\n- **Is it slow on a realistic input?**\n  A profile of a toy input measures the toy.\n- **What would \"fast enough\" be?**\n  Without a target there is no way to stop, and the loop runs until the code\n  is unreadable.\n\nIf the answer to the first question is \"no\", say so and stop.\nThat is a complete, correct result for this skill, not a failure to deliver.\n\n### 2. Fix the input and a correctness baseline\n\nPut the code under measurement in its own file and `source()` it, so the\nprofiler can link its samples back to source lines.\n\nRecord what the current code returns on the realistic input.\nEvery alternative gets checked against it later:\n\n```r\nsource(\"\u003Cscript>.R\")\nbaseline \u003C- \u003Ccall-under-test>\nsaveRDS(baseline, \"baseline.rds\")\n```\n\nSpeed is only interesting among alternatives that agree.\n`bench::mark()` enforces that in step 5, but a saved baseline also catches an\nalternative that changes results in a way the benchmark never evaluates.\n\n### 3. Profile: locate the bottleneck, do not guess\n\nR's profiler is a sampling profiler: it stops execution every few\nmilliseconds and records the call stack.\nThat keeps overhead low at the cost of being stochastic, so successive\nprofiles differ slightly.\nThe variability mostly affects functions that take very little time, which\nare the ones you care least about.\n\nThe interactive route, which links the profile back to source lines:\n\n```r\nsource(\"\u003Cscript>.R\")\np \u003C- profvis::profvis(\u003Ccall-under-test>)\n```\n\nIf the call finishes before any sample lands, profvis aborts with\n`No parsing data available. Maybe your function was too fast?`.\nThat is the profiler telling you the input is not realistic enough to profile,\nso go back to step 1 rather than shrinking the sampling interval.\n\nIn a headless session, save the widget and open it later:\n\n```r\nhtmlwidgets::saveWidget(p, \"profile.html\", selfcontained = TRUE)\n```\n\n`selfcontained = TRUE` needs pandoc, and htmlwidgets discovers it through\n`{rmarkdown}`: as of htmlwidgets 1.6.0 that path \"now uses the `{rmarkdown}`\npackage to discover and call pandoc\"\n([NEWS](https:\u002F\u002Fgithub.com\u002Framnathv\u002Fhtmlwidgets\u002Fblob\u002Fmaster\u002FNEWS.md)).\n`rmarkdown::pandoc_available()` is therefore the gate that matches what\nhtmlwidgets itself consults, rather than a proxy for it.\nWithout pandoc the call aborts rather than degrading quietly, with\n`Saving a widget with selfcontained = TRUE requires pandoc.` (verified on\nhtmlwidgets 1.6.4).\nDrop the argument to `FALSE` in that case, which writes a sidecar\ndependencies directory next to the HTML instead.\n\nWhen there is no way to view HTML at all, take the text summary instead:\n\n```r\nsource(\"\u003Cscript>.R\")\ntmp \u003C- tempfile()\nRprof(tmp, interval = 0.01, memory.profiling = TRUE)\n\u003Ccall-under-test>\nRprof(NULL)\nsummaryRprof(tmp, memory = \"both\")$by.self\n```\n\n`by.self` ranks functions by time spent in the function itself rather than in\nits callees, which is what points at the line to change.\n\nUse `profvis::pause()`, never `Sys.sleep()`, when building a synthetic\nexample to reason about: as far as R can tell, `Sys.sleep()` uses no\ncomputing time, so it never appears in the profile.\n\n### 4. Read the profile\n\nThe flame graph shows the full call stack, so a function called from two\nplaces is visible as two stacks rather than one aggregate.\nWatch for two things in particular.\n\n**A function high in the self-time ranking because it is called often, not\nbecause it is slow.**\nThe fix is the call count, not the function.\n\n**`\u003CGC>`.**\nThis entry is the garbage collector, not your code.\nA lot of time in `\u003CGC>` almost always means many short-lived objects, and the\nusual cause is copy-on-modify in a loop that grows an object one element at a\ntime:\n\n```r\nx \u003C- integer()\nfor (i in 1:1e4) x \u003C- c(x, i)     # every iteration copies all of x\n```\n\nConfirm it from the memory column: a line that allocates and frees large\namounts on every pass is the one to fix, and the fix is preallocation, not a\nfaster arithmetic operator.\n\n### 5. Microbenchmark only the bottleneck\n\n`bench::mark()` uses a high-precision timer, so it can separate operations\nthat take microseconds.\nBenchmark the one expression the profile implicated, with the realistic input\nfrom step 2, not the whole pipeline:\n\n```r\nx \u003C- runif(100)\nlb \u003C- bench::mark(\n  sqrt(x),\n  x^0.5\n)\nlb[c(\"expression\", \"min\", \"median\", \"itr\u002Fsec\", \"n_gc\", \"mem_alloc\")]\n```\n\nBy default each expression runs at least once (`min_iterations = 1`) and then\nas many times as fit in half a second (`min_time = 0.5`).\n\n`bench::mark()` checks that every expression returns the same value and\naborts if they differ:\n\n```text\nError : Each result must equal the first result:\n`sum(x)` does not equal `mean(x)`\n```\n\nThat error is usually the benchmark catching a real bug in the alternative.\nSet `check = FALSE` only when the expressions are *meant* to return different\nthings, and say in the report why.\n\nUse `bench::press()` when the answer depends on input size, so the comparison\nruns across a grid of sizes rather than at one arbitrary point.\n\n### 6. Interpret the numbers\n\n**Read `min` and `median`, not the mean.**\nThe timing distribution is heavily right-skewed, and often multimodal because\nthe machine is doing other things.\n`min` is the best achievable time and `median` the typical one;\n`plot(lb)` shows the distribution on a log x-axis.\nThe returned tibble carries `expression`, `min`, `median`, `itr\u002Fsec`,\n`mem_alloc`, `gc\u002Fsec`, `n_itr`, `n_gc`, `total_time`, and the list-columns\n`result`, `memory`, `time`, and `gc` (verified against bench 1.1.4).\n\n**Report absolute units, not just a ratio.**\n\"2.1x faster\" is not actionable on its own.\nCalibrate with how many calls it takes to reach one second:\n\n| Per call | Calls per second of run time |\n|---|---|\n| 1 ms | one thousand |\n| 1 us | one million |\n| 1 ns | one billion |\n\nSo an expression at 1.4 us that the real workload calls a few hundred times\ncannot account for a slow job, however large its ratio, and swapping it is\nchurn.\n\n**Check `mem_alloc` and `n_gc` alongside the times.**\nWhen step 4 flagged `\u003CGC>`, these are the columns that show whether the\nalternative actually allocates less.\n\n### 7. Re-profile at real scale\n\nA microbenchmark measures a snippet in isolation.\nReal code is dominated by higher-order effects, so a microbenchmark win is a\nhypothesis about the real workload, not a result about it.\nApply the change, re-run step 3 on the full realistic workload, and compare\nagainst the original wall-clock time from step 1.\n\nIf the end-to-end time did not move, revert the change.\nA faster expression that leaves the job the same length has bought nothing\nand cost readability.\n\n### 8. Report\n\nState, in this order:\n\n1. The workload, its input, and its before\u002Fafter wall-clock time.\n2. The bottleneck the profile identified, and how much of total time it held.\n3. The `bench::mark()` table for the alternatives, with units.\n4. Whether the end-to-end re-profile confirmed the win.\n5. Which limitations below apply, if any.\n\nAttach the numbers to the PR when the change ships, so the next reviewer does\nnot have to re-derive them.\n\n## Limitations to state in the report\n\nThe profiler cannot see everything, and each gap silently misattributes time:\n\n- **Compiled code.**\n  Profiling shows that R called into C\u002FC++ but not what happened inside.\n  Say so rather than reporting the C call as a leaf.\n- **Anonymous functions.**\n  Heavy functional-programming code is hard to attribute call by call.\n  Name the functions before profiling.\n- **Lazy evaluation.**\n  An argument is evaluated where it is first used, so in `j(i())` the profile\n  attributes `i()`'s cost to `j()`.\n  Use `force()` to pull evaluation forward when the attribution matters.\n\n## Outside R\n\nThe tools are R-specific; the order is not.\nProfile before optimizing, microbenchmark only the bottleneck the profile\nfound, read medians rather than means, and confirm the win end to end.\nIn Python the stdlib equivalents of the two measurement steps are `cProfile`\nand `timeit`.\n\n## Relationship to other skills\n\n- [`algorithmatize-checks`](..\u002F..\u002Fshared\u002Fworkflow\u002Falgorithmatize-checks.md) --\n  the general rule this skill instruments: a performance claim is decidable by\n  measurement, so never settle it by reasoning.\n- [`dont-reinvent-wheel`](..\u002F..\u002Fshared\u002Fprinciples\u002Fdont-reinvent-wheel.md) and\n  [`prefer-packaged-functions`](..\u002F..\u002Fshared\u002Fcoding\u002Fprefer-packaged-functions.md)\n  -- check for an existing, usually C-backed, packaged implementation before\n  hand-optimizing.\n  A found package beats a won benchmark.\n- [`use-memoisation`](..\u002F..\u002Fshared\u002Fcoding\u002Fuse-memoisation.md) -- one of the\n  fixes this skill's profile can point at, and the fragment that says to raise\n  a missed memoisation only when the function is \"demonstrably hot\".\n  Step 3 is what demonstrates it, and step 6's `mem_alloc` column is where the\n  memory half of the trade shows up.\n- [`reprexes`](..\u002Freprexes\u002FSKILL.md) -- reduces a slow workload to the\n  minimal self-contained snippet step 5 benchmarks.\n- [`test`](..\u002Ftest\u002FSKILL.md) and [`r-pkg-check`](..\u002Fr-pkg-check\u002FSKILL.md) --\n  the correctness gates an optimization still has to pass.\n  `bench::mark()`'s equality check compares the benchmarked expressions to\n  each other, not the package to its test suite.\n- [`simplify`](..\u002Fsimplify\u002FSKILL.md) and [`tidy`](..\u002Ftidy\u002FSKILL.md) -- an\n  optimization step 7 could not confirm is complexity debt; hand the cleanup\n  to these.\n- [`ardi`](..\u002Fardi\u002FSKILL.md) -- the loop that carries step 8's numbers into a\n  review round, whether the finding is yours or a reviewer's.\n\n## Anti-patterns\n\n- Optimizing a line because it looks slow, with no profile.\n- Profiling a toy input, then acting on the result as if it described the\n  real workload.\n- Microbenchmarking the whole pipeline instead of the bottleneck the profile\n  named.\n- Comparing means, or reporting a ratio with no absolute units behind it.\n- Passing `check = FALSE` to silence a genuine difference in results, rather\n  than because the expressions are meant to differ.\n- Shipping a microbenchmark win without the end-to-end re-profile of step 7.\n- Reading `\u003CGC>` as slow code rather than as an allocation problem.\n- Using `Sys.sleep()` in a synthetic profiling example, where it is invisible\n  to the profiler.\n- Asserting a speedup in a PR body with no numbers attached.\n",{"name":5,"description":7,"allowed-tools":24,"user-invocable":31},[25,26,27,28,29,30],"Bash","Read","Edit","Write","Grep","Glob",true,"main","0f720481f8ce3b3d9fef485f6e230d8b3035748e","skills\u002Fmeasure-performance",1,"https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config","https:\u002F\u002Fmorrison-lab.github.io\u002Fai-config\u002F","https:\u002F\u002Fgithub.com\u002FMorrison-Lab","2026-11-15 04:39:15",{"source":8,"skillName":5,"commands":41,"prompt":51,"zipAvailable":31},[42,45,48],{"tool":43,"command":44},"npx","npx skills add Morrison-Lab\u002Fai-config --skill measure-performance",{"tool":46,"command":47},"bunx","bunx skills add Morrison-Lab\u002Fai-config --skill measure-performance",{"tool":49,"command":50},"pnpm","pnpm dlx skills add Morrison-Lab\u002Fai-config --skill measure-performance","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fmeasure-performance\u002FSKILL.md",[],[54,61,68,75,82,89],{"skillNo":55,"name":56,"nameZh":6,"description":57,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":58,"categories":59,"keywords":60,"publishedAt":19},"01M735BVYCGR5VEC65AFDVSNKW","wrap-up","End-of-session verification and UMS.","https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fwrap-up\u002FSKILL.md",[],[],{"skillNo":62,"name":63,"nameZh":6,"description":64,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":65,"categories":66,"keywords":67,"publishedAt":19},"01M735BTH469T1CQ8G2Q9GENCR","workaround-watcher","Watch upstream issue for fix.","https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fworkaround-watcher\u002FSKILL.md",[],[],{"skillNo":69,"name":70,"nameZh":6,"description":71,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":72,"categories":73,"keywords":74,"publishedAt":19},"01M735BRKZWFANSQ0GM380B4TD","wn","→ whats-next.","https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fwn\u002FSKILL.md",[],[],{"skillNo":76,"name":77,"nameZh":6,"description":78,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":79,"categories":80,"keywords":81,"publishedAt":19},"01M735BPE9V830B3B6NGXQ56J6","whats-next","Recommend concrete next step or action.","https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fwhats-next\u002FSKILL.md",[],[],{"skillNo":83,"name":84,"nameZh":6,"description":85,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":86,"categories":87,"keywords":88,"publishedAt":19},"01M735BMHQ60EAD5B7QAWXC5A0","wait-for-results","Wait for pipeline\u002FCI results.","https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fwait-for-results\u002FSKILL.md",[],[],{"skillNo":90,"name":91,"nameZh":6,"description":92,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":93,"categories":94,"keywords":95,"publishedAt":19},"01M735BKGZHPN1PEMJ7YBD5VJ8","wai","→ send-notes.","https:\u002F\u002Fgithub.com\u002FMorrison-Lab\u002Fai-config\u002Fblob\u002Fmain\u002Fskills\u002Fwai\u002FSKILL.md",[],[]]