agentsclimarketplace

Knuths optimization principle

Skill The-Artificer-of-Ciphers-LLC/skills-from-the-artificer/knuths-optimization-principle

29 drop-in Claude Code skills: cost-tier model routing, debugging (rubber-duck, test-first-bugfix, trust-but-verify), and 24 laws-of-software reference skills. Install with: npx skills add The-Artificer-of-Ciphers-LLC/skills-from-the-artificer --all

Install
npx -y skills add The-Artificer-of-Ciphers-LLC/skills-from-the-artificer --skill knuths-optimization-principle

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 3 stars3 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its author says it does

Copied from the file, not written here

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.

SKILL.md

4.3 KB, as published. Nobody here has run it

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

  1. Have we actually measured that this is a bottleneck, or are we optimizing on intuition?
  2. What is the concrete performance goal? Are we currently failing to meet it?
  3. Does this optimization make the code harder to understand? Is the performance gain worth that cost?
  4. Have we profiled first to confirm this is where the time is actually being spent?

Keep looking

Skills are one crate of 328,083. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.