knuths-optimization-principle
Skill6 starsChanged 33 days agoMITView on GitHub
Apply Knuth's Optimization Principle when someone wants to optimize code before establishing it's a bottleneck, is using complex data structures for perceived performance, is making code harder to read "for speed", or is debating whether to optimize something that hasn't been profiled. Trigger on phrases like "I should make this faster", "let's pre-optimize this", "I used X instead of Y for performance", "this might be slow", or any discussion where performance is the motivation for added complexity that hasn't been validated by measurement.
In this repository79 other files it gives its agents.
- adr-phase-coverageSkill
- aposd-red-flag-reviewSkill
- apple-on-device-aiSkill
- artifact-gatesSkill
- asc-app-create-uiSkill
- asc-build-lifecycleSkill
- asc-cli-usageSkill
- asc-crash-triageSkill
- asc-id-resolverSkill
- asc-localize-metadataSkill
- asc-metadata-syncSkill
- asc-notarizationSkill
- asc-ppp-pricingSkill
- asc-release-flowSkill
- asc-revenuecat-catalog-syncSkill
- asc-shots-pipelineSkill
- asc-signing-setupSkill
- asc-submission-healthSkill
- asc-subscription-localizationSkill
- asc-testflight-orchestrationSkill
- asc-wall-submitSkill
- asc-workflowSkill
- asc-xcode-buildSkill
- choose-boring-technologySkill
- ci-preflightSkill
- conways-lawSkill
- cost-routingSkill
- cost-tier-routingSkill
- cunninghams-lawSkill
- ddd-strategic-auditSkill
- ddd-tactical-reviewSkill
- debug-test-captureSkill
- developing-ios-appsSkill
- doerrs-lawSkill
- fitts-lawSkill
- fix-regression-guardSkill
- galls-lawSkill
- goodharts-lawSkill
- greenspuns-tenth-ruleSkill
- gsd-core-hooksSkill
- hofstadters-lawSkill
- hyrums-lawSkill
- ios-accessibilitySkill
- ios-simulator-boot-healthSkill
- kerckhoffs-principleSkill
- kernighans-lawSkill
- lady-lovelaces-objectionSkill
- leaky-abstractionsSkill
- linuss-lawSkill
- mobile-ios-designSkill
- money-boundary-judgmentSkill
- moores-lawSkill
- no-deferSkill
- norvigs-lawSkill
- parkinsons-lawSkill
- peter-principleSkill
- postels-lawSkill
- post-fix-traceSkill
- qa-test-architectSkill
- rubber-duckSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 6 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 22
Don't optimize before you've measured.
- Distinctive: line 23
Don't optimize the 97% that doesn't matter.
- Distinctive: line 24
Do optimize the 3% that actually is the bottleneck — carefully and deliberately.
- Distinctive: line 42
When performance is actually a problem — users are complaining, SLOs are being missed, latency is measurable — profile the code.
- Distinctive: line 48
After optimizing, measure again.
- Distinctive: line 51
Before micro-optimizing, check: is there a better algorithm?
≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Knuth's Optimization Principle
"Premature optimization is the root of all evil." — Donald Knuth, 1974 (often attributed; the full quote is more nuanced)
The core idea
Optimizing code before you know it's a bottleneck is one of the most common and costly mistakes in software development. It makes code harder to read, harder to change, and harder to debug — all in service of a performance problem that may not exist, or that may not be in the place you're optimizing.
The full Knuth quote
The complete passage is important and often missed:
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."
The principle is not "never optimize." It's:
- Don't optimize before you've measured.
- Don't optimize the 97% that doesn't matter.
- Do optimize the 3% that actually is the bottleneck — carefully and deliberately.
Why premature optimization is harmful
It optimizes the wrong things. Human intuition about where code is slow is notoriously unreliable. Profilers routinely reveal that the bottleneck is somewhere nobody expected. Optimizing your hunch is usually wasted effort.
It adds complexity without benefit. Optimization techniques — caching, pooling, bitwise tricks, manual memory management, loop unrolling — make code harder to read, maintain, and debug. If there's no performance problem to justify them, you've paid the cost for nothing.
It constrains future design. Premature optimization often locks in architectural decisions that are hard to reverse. You've optimized yourself into a corner before understanding the real constraints.
It's a distraction. Time spent on premature optimization is time not spent on correctness, features, or the actual bottlenecks.
The right process
1. Write correct, clear code first. Make it work. Make it obvious. Make it readable. Don't sacrifice clarity for hypothetical performance.
2. Measure before optimizing. When performance is actually a problem — users are complaining, SLOs are being missed, latency is measurable — profile the code. Find the actual bottleneck. Don't guess.
3. Optimize only the bottleneck. The profiler will show you where time is actually being spent. Optimize that — not what you assumed would be slow.
4. Verify the optimization works. After optimizing, measure again. Confirm the change actually improved performance. Optimizations sometimes don't help, or help in benchmarks but not in production.
5. Consider algorithmic improvements first. Before micro-optimizing, check: is there a better algorithm? Going from O(n²) to O(n log n) is usually worth more than any micro-optimization, and often doesn't sacrifice clarity.
When optimization early is fine
- Choosing data structures and algorithms at design time. Picking a hash map over a linear search is not premature — it's good design. The principle is about micro-optimization, not algorithmic choice.
- Performance-critical paths that are known in advance. Real-time systems, game engines, signal processing — if you know the constraints, design for them.
- Optimization that doesn't cost clarity. Some optimizations are free: obvious algorithmic improvements, using built-in optimized library functions, etc.
Key questions to surface
- Have we actually measured that this is a bottleneck, or are we optimizing on intuition?
- What is the concrete performance goal? Are we currently failing to meet it?
- Does this optimization make the code harder to understand? Is the performance gain worth that cost?
- Have we profiled first to confirm this is where the time is actually being spent?
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

