Efficiency & Trade-Offs
Not all solutions to a problem are equally good. Two approaches can both produce the correct result, but one might be dramatically faster, cheaper, or simpler. Understanding efficiency and trade-offs means recognizing that every choice has costs and benefits — and choosing wisely.
This chapter explores how to think about efficiency in everyday life and in technology, and introduces the basic concepts behind Big O notation.
Every Solution Has a Cost
When you solve a problem, you spend resources. Those resources might be time, money, energy, space, or attention. A solution that works but uses too many resources is not really a good solution.
The key insight is that you rarely optimize for just one thing. Instead, you make trade-offs: gaining something in one area by giving something up in another.
Everyday Trade-Offs
Highway vs Side Streets
Driving to work, you have choices:
Highway route:
- Distance: 15 miles
- Time (no traffic): 15 minutes
- Time (rush hour): 45 minutes
- Gas cost: moderate
Side street route:
- Distance: 8 miles
- Time (no traffic): 20 minutes
- Time (rush hour): 25 minutes
- Gas cost: lower (but more stop-and-go)
The highway is faster when traffic is light — but during rush hour, the side streets win. The "best" route depends on the conditions. This is a trade-off between distance, time, and predictability.
Batch Cooking vs Cooking Daily
Batch cooking (meal prep on Sunday):
- Time spent: 3 hours once per week
- Daily time: 10 minutes to reheat
- Freshness: decreases over the week
- Variety: limited (you eat the same meals)
Cooking daily:
- Time spent: 30-45 minutes each day
- Weekly total: 3.5 to 5.25 hours
- Freshness: maximum
- Variety: whatever you want each day
Batch cooking trades freshness and variety for convenience and time savings. Neither approach is universally better — it depends on what you value.
Buying in Bulk vs Buying as Needed
Buying in bulk:
- Lower price per unit
- Requires more storage space
- Risk of waste if items expire
- Fewer trips to the store
Buying as needed:
- Higher price per unit
- Minimal storage needed
- Less waste
- More frequent trips
This is a trade-off between money, space, and time. A family of six benefits from bulk buying. A single person in a small apartment probably does not.
What Efficiency Means
Efficiency is about getting the best result from the resources you invest. But "best" always needs context:
- Time efficiency: How quickly does it finish?
- Space efficiency: How much storage or room does it need?
- Cost efficiency: How much money does it require?
- Energy efficiency: How much effort does it take?
A solution can be efficient in one dimension and wasteful in another. A fast delivery service (time-efficient) might use individual deliveries instead of batched routes (cost-inefficient). Efficiency always asks: "efficient with respect to what?"
Efficiency in Technology
Why It Matters
When a program processes ten items, almost any approach works fine. When it processes ten million items, the approach matters enormously.
Approach A: Check every item one by one
- 10 items: instant
- 10,000 items: 0.1 seconds
- 10,000,000 items: 100 seconds (almost 2 minutes)
Approach B: Use a smart lookup structure
- 10 items: instant
- 10,000 items: instant
- 10,000,000 items: instant (about 23 lookups)
The difference is negligible at small scales but massive at large ones. This is why efficiency matters in technology — systems need to work at scale.
Time Complexity
Time complexity describes how the time an algorithm takes grows as the input gets larger. It is not about measuring exact seconds — it is about the pattern of growth.
Constant: Always takes the same time, regardless of input size.
Example: Looking up a word in a dictionary by page number.
Linear: Time grows proportionally with input size.
Example: Reading every page of a book to find a word.
Logarithmic: Time grows very slowly as input increases.
Example: Binary search — opening the dictionary to the
middle, then halving the remaining section each time.
Quadratic: Time grows with the square of the input size.
Example: Comparing every student's paper with every other
student's paper to check for plagiarism.
Space Complexity
Space complexity describes how much memory or storage an algorithm needs as the input grows.
Sometimes you can trade space for time:
Scenario: You need to check if a customer has visited your store before.
Approach A (time-focused):
- Keep a list of all previous customers in memory.
- Check instantly by looking up the list.
- Uses more memory (space), but very fast (time).
Approach B (space-focused):
- Do not keep a list.
- Search through the transaction history each time.
- Uses little memory, but much slower.
This time-space trade-off is one of the most fundamental in computing.
Big O Basics
Big O notation is a way to describe time or space complexity concisely. It focuses on the dominant factor — the one that matters most when the input gets very large.
O(1) Constant Same time regardless of size
O(log n) Logarithmic Very slow growth
O(n) Linear Grows proportionally
O(n log n) Log-linear Slightly faster than proportional
O(n^2) Quadratic Grows with the square
O(2^n) Exponential Doubles with each additional element
A practical way to think about it:
If you have 1,000 items:
O(1) = 1 operation
O(log n) = ~10 operations
O(n) = 1,000 operations
O(n log n) = ~10,000 operations
O(n^2) = 1,000,000 operations
O(2^n) = more operations than atoms in the universe
You do not need to memorize formulas. The important takeaway is that the growth pattern matters far more than the exact number of operations, especially as inputs get large.
Real-World Technology Trade-Offs
Speed vs Accuracy
A spell checker could scan every word against a complete dictionary for perfect accuracy, but it would be slow. Instead, it uses approximate matching that is fast and correct 99% of the time. The 1% trade-off is worth the speed gain.
Storage vs Computation
Streaming services face a choice: store every possible video quality version of a movie (using lots of storage), or store one version and convert it on the fly when someone watches (using lots of computation). Most use a middle ground — storing a few common formats and converting for edge cases.
Simplicity vs Flexibility
A simple solution that handles 90% of cases is often better than a complex one that handles 100%. The extra 10% might require three times the effort to build and maintain. Whether that trade-off is worth it depends on the consequences of not handling those cases.
Simple approach:
- Handles 90% of cases
- Takes 1 week to build
- Easy to maintain and understand
Complex approach:
- Handles 100% of cases
- Takes 4 weeks to build
- Harder to maintain, more potential for bugs
How to Think About Trade-Offs
Ask these questions when evaluating solutions:
- What resources does each approach use? (time, space, money, effort)
- How does each approach scale? (works for 10 items — what about 10,000?)
- What is being sacrificed? (every gain has a cost somewhere)
- What matters most in this context? (speed? cost? simplicity? accuracy?)
- What is "good enough"? (perfect is often the enemy of done)
Common Pitfalls
Optimizing too early
Improving efficiency before you know where the bottleneck is wastes effort. A common saying in software: "Premature optimization is the root of all evil." First make it work, then make it fast — and only the parts that need to be fast.
Optimizing the wrong thing
Spending hours making a process 10% faster when it only runs once a month is a poor use of time. Focus optimization efforts on things that run frequently or handle large amounts of data.
Ignoring trade-offs
Claiming a solution is "better" without specifying in what dimension is meaningless. Better at what? Faster but uses more memory? Cheaper but less reliable? Always name the trade-off.
Assuming more resources is always the answer
Sometimes the answer to "this is slow" is not "buy a faster computer" but "use a better algorithm." A better algorithm can turn a process that takes hours into one that takes seconds, on the same hardware.
Chasing perfection
A solution that is 95% efficient and ships today beats a solution that is 100% efficient and ships never. Know when "good enough" is genuinely good enough.
Key Takeaways
- Every solution involves trade-offs — gaining in one area while giving up in another.
- Efficiency means getting the best result for the resources invested, and it always requires specifying "efficient with respect to what?"
- In everyday life, trade-offs appear in route planning, cooking, shopping, and virtually every decision.
- In technology, time complexity and space complexity describe how algorithms scale with input size.
- Big O notation captures the growth pattern of an algorithm, which matters far more than exact measurements at large scale.
- The best approach depends on context: what resources you have, what scale you need to handle, and what matters most for your situation.