Premature Optimisation
Also Known As
early optimisation
premature optimization
Knuth rule
TL;DR
Optimising code before measuring where the actual bottleneck is — trading readability for performance gains that often don't matter.
Explanation
Donald Knuth's famous observation — 'premature optimisation is the root of all evil' — warns against sacrificing code clarity for performance improvements that are speculative or negligible. Most performance problems lie in a small fraction of the code; optimising the wrong parts wastes time and produces harder-to-maintain code. The correct approach: write clear, correct code first; measure with a profiler to find real bottlenecks; optimise only what the data confirms is slow. In PHP, micro-optimisations (avoiding function calls, hand-rolling loops) almost never matter — I/O and query patterns almost always do.
Common Misconception
✗ Premature optimisation means you should never think about performance until the end. It means not optimising before you have measured — designing for reasonable algorithmic complexity from the start is not premature optimisation, it is good engineering.
Why It Matters
Optimising before profiling makes code harder to read and maintain in exchange for performance improvements that are often unmeasurable — the bottleneck is almost never where developers intuit it is.
Common Mistakes
- Micro-optimising loop constructs (for vs foreach vs while) without benchmarking actual impact.
- Using static methods or avoiding OOP for 'performance' in application code where the overhead is negligible.
- Caching results before measuring whether the operation is actually slow.
- Choosing a complex algorithm over a simple one for scale that the application will never reach.
Code Examples
💡 Note
"Premature optimisation is the root of all evil" — Knuth. Profile first, then optimise the measured bottleneck.
✗ Vulnerable
// Micro-optimised before profiling — complex and wrong
public function getActiveUsers(): array {
// Avoid array_filter — 'too slow'
\$result = [];
\$users = \$this->users;
\$count = count(\$users); // cache count 'for speed'
for (\$i = 0; \$i < \$count; \$i++) {
if (\$users[\$i]->active) \$result[] = \$users[\$i];
}
return \$result;
}
✓ Fixed
// Write the clear version first — profile before optimising
public function getActiveUsers(): array {
return array_values(array_filter(\$this->users, fn(\$u) => \$u->active));
}
// If this is actually slow (profiler shows it) — fix the query, not the PHP:
// SELECT * FROM users WHERE active = 1 (let the DB filter, not PHP)
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
28
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Amazonbot 8
Perplexity 4
Unknown AI 3
Ahrefs 2
ChatGPT 2
Majestic 1
Google 1
SEMrush 1
Also referenced
How they use it
crawler 20
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Low
⚡ Quick Fix
Write readable correct code first; profile to find actual bottlenecks; optimise only the hot paths that profiling identifies — the 3% that matters
📦 Applies To
any
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Micro-optimisations (isset vs array_key_exists) without profiling data; complex caching of rarely-called functions; bitwise operations for simple booleans
Auto-detectable:
✗ No
blackfire
xdebug
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: High
Context: File
Tests: Update