Recognizing
Optimization Needs
Learn to identify when your Python algorithm is too slow and understand the concepts behind making it faster.
What Is Algorithm Efficiency?
Algorithm efficiency measures how the running time or memory usage of a program grows as the input size increases. Two programs can produce the same correct output, but one might take seconds while the other takes hours on large data.
Introducing Big-O Notation
Big-O notation describes the worst-case growth rate of an algorithm. We write \( O(f(n)) \) to say "this algorithm's time grows at most proportionally to \( f(n) \) for large \( n \)."
Visualizing Growth Rates
The Core Rule: Counting Nested Loops
The most common source of slow algorithms in Python is nested iteration. Each additional loop that iterates over the input multiplies the total work:
When Is "Too Slow"?
A modern computer can execute roughly \( 10^8 \) simple operations per second. Use this rule of thumb to judge whether your algorithm can finish in time:
Correct vs. Efficient
A correct algorithm produces the right answer. An efficient algorithm produces it fast. The first step is always correctness, but when dealing with large inputs, efficiency becomes critical.
| Correct but Slow | Correct and Fast | |
|---|---|---|
| Approach | Brute-force: try everything | Clever: exploit structure |
| Small input (\( n = 100 \)) | ✓ Works fine | ✓ Works fine |
| Large input (\( n = 10^6 \)) | ✕ Times out | ✓ Finishes quickly |
| Priority | Good starting point | Goal for production code |
Red Flags: Signs You Need Optimization
for i in range(n) inside another for j in range(n) means \( O(n^2) \).x in list inside a loop is \( O(n) \) per check: \( O(n^2) \) total.How to Diagnose a Slow Algorithm
in on lists, .index(), list slicing, or string concatenation.Example 1: Finding Duplicates
Example 2: Hidden \( O(n) \) Inside a Loop
Example 3: Recursive Explosion
Common Optimization Strategies
Common Mistakes
in on a list inside a loop looks like one line of code but adds an entire \( O(n) \) scan per iteration.Summary: Complexity at a Glance
| Complexity | Operations at \( n = 10,000 \) | Fast Enough? | Common Pattern |
|---|---|---|---|
| \( O(1) \) | 1 | ✓ Always | Dict lookup |
| \( O(\log n) \) | ~13 | ✓ Always | Binary search |
| \( O(n) \) | \( 10^4 \) | ✓ Yes | Single loop |
| \( O(n \log n) \) | ~\( 1.3 \times 10^5 \) | ✓ Yes | Sorting |
| \( O(n^2) \) | \( 10^8 \) | ~ Borderline | Nested loops |
| \( O(2^n) \) | \( 10^{3010} \) | ✕ Never | Naive recursion |
Question 1 of 5
You have a list of \( n = 50,000 \) numbers and a for loop that checks if x in my_list for each element. What is the time complexity?
in operator on a list is \( O(n) \). Inside a loop of \( n \) iterations, the total is \( O(n) \times O(n) = O(n^2) \).Question 2 of 5
Which of the following is the best reason to optimize an algorithm?
Question 3 of 5
A function has three nested loops, each iterating over a list of size \( n \). What is the time complexity?
Question 4 of 5
You convert a list to a set before a loop that checks membership. What changes?
Question 5 of 5
The naive recursive Fibonacci function fib(n) calls itself twice at each step. Approximately how many function calls does fib(40) make?