Algorithmic Improvements
vs. Micro-Optimizations
Learn to recognize when your code needs a better algorithm versus a faster implementation: the skill that separates good engineers from great ones.
What Is Performance Optimization?
Performance optimization means making code run faster or use less memory. But not all optimizations are created equal. There are two fundamentally different strategies:
Why Big-O Dominates at Scale
As input size \(n\) grows, the order of growth overwhelms any constant-factor speedup. A micro-optimization might make your code 2x faster, but an algorithmic improvement can make it thousands of times faster.
An Analogy: Finding a Word
Imagine searching for a word in a dictionary with \(n = 100{,}000\) entries:
The Core Rule
Growth Rates: Numbers Don't Lie
| \(n\) | \(O(\log n)\) | \(O(n)\) | \(O(n \log n)\) | \(O(n^2)\) |
|---|---|---|---|---|
| 10 | 3 | 10 | 33 | 100 |
| 1,000 | 10 | 1,000 | 10,000 | 1,000,000 |
| 100,000 | 17 | 100,000 | 1,700,000 | 10,000,000,000 |
| 10,000,000 | 23 | 10M | 233M | 100 trillion |
Algorithmic vs. Micro: Side by Side
| Aspect | Algorithmic Improvement | Micro-Optimization |
|---|---|---|
| Changes Big-O? | Yes | No |
| Example | Bubble Sort → Merge Sort | i++ → ++i |
| Impact at small \(n\) | Often minimal | Can matter |
| Impact at large \(n\) | Dramatic | Marginal |
| Effort | Redesign approach | Tweak existing code |
| When to apply | First | After profiling |
How to Decide Which You Need
The Decision Flowchart
Example 1: Finding Duplicates
Problem: Given an array of \(n\) integers, determine if any value appears more than once.
Example 2: Sorting 1 Million Items
With \(n = 1{,}000{,}000\), compare two strategies:
When Micro-Optimizations Do Matter
Once your algorithm is optimal, constant factors can matter for performance-critical code. Here is a real Java example:
True Micro-Optimizations in Java
These changes do not alter Big-O, but reduce constant factors:
ArrayList vs. LinkedList for sequential access: same \(O(n)\) traversal, but ArrayList has better cache locality.new ArrayList<>(1000) avoids internal array resizing, saving a constant factor.array.length in a local variable before a loop: avoids repeated field access.int) over wrappers (Integer): avoids boxing/unboxing overhead.Example: Shortest Path
Problem: Find the shortest path in a weighted graph with \(V\) vertices and \(E\) edges.
Common Mistakes
ArrayList for frequent contains() checks: that is \(O(n)\) per call. A HashSet makes it \(O(1)\).Summary Cheat Sheet
| Symptom | Likely Fix | Type |
|---|---|---|
| Runtime doubles when input doubles | \(O(n)\) is fine: profile constants | Micro |
| Runtime quadruples when input doubles | You have \(O(n^2)\): find a better algorithm | Algorithmic |
| Works for 100 items, breaks at 10,000 | Almost certainly a Big-O problem | Algorithmic |
| Consistently 2x slower than expected | Profile: maybe bad data structure or I/O | Micro |
| Nested loops over the same collection | Can you use a HashMap/HashSet? | Algorithmic |
| Lots of object creation in a tight loop | Reuse objects or use primitives | Micro |
Question 1 of 5
A program searches for a value in an unsorted array using a linear scan (\(O(n)\)). A developer proposes "unrolling" the loop to check 4 elements per iteration. This is an example of:
Question 2 of 5
A method checks if a sorted array contains a target by iterating from index 0 to \(n-1\). To improve performance, you should first:
Question 3 of 5
A program counts how many pairs \((i, j)\) in an array sum to a target \(k\) using two nested loops. Which change is an algorithmic improvement?
Question 4 of 5
Your code already uses an optimal \(O(n \log n)\) sorting algorithm, but it's still slower than expected. What should you do next?
Question 5 of 5
Which of the following is the best summary of when to apply micro-optimizations?