Optimize only after identifying the costly operation.
Understanding the Concept
Optimize only after identifying the costly operation. List append is efficient for building a list; join is preferable for concatenating many strings; sets are useful for repeated membership tests.
Algorithmic complexity and data structure choice often matter more than tiny syntax differences.
How to interpret the measurement
Before optimizing, establish an objective measurement and a representative workload. Memory size, function-call counts, elapsed time and samples are different metrics: knowing one does not determine the others. Repeat observations before drawing a conclusion about a performance regression.
Profiling output is evidence for finding a bottleneck, not a performance guarantee. Check that the program still produces the correct result after any optimization and note version, input size and measurement conditions.
Worked Example
Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.
items = [f"item-{number}" for number in range(200)]
requested = {"item-5", "item-150"}
selected = [item for item in items if item in requested]
message = ", ".join(selected)
print(message)
Walkthrough: What Each Part Does
- The coordination call waits for a result or a completion condition. Predict what would happen if the worker failed or never became ready.
- The printed number or profile is diagnostic data. Repeat the observation and identify the unit before comparing values.
This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.
Example Output
item-5, item-150
The output above was captured by running the example in the available Python environment. The order of concurrent events, durations and platform-dependent values can legitimately differ on your computer.
Important Behavior and Edge Cases
Replacing clear code with complicated micro-optimizations without measuring the original bottleneck.
Algorithmic complexity and data structure choice often matter more than tiny syntax differences.
Practice Exercise
Compare joining strings with += in a loop over increasingly large inputs.
Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.
Further reading: official documentation and API reference.
Common Beginner Mistakes
- Replacing clear code with complicated micro-optimizations without measuring the original bottleneck.
- Comparing results from different workloads or environments as if the measurements were directly interchangeable.
Key Takeaways
- Optimize only after identifying the costly operation.
- Algorithmic complexity and data structure choice often matter more than tiny syntax differences.
- Practice: Compare joining strings with += in a loop over increasingly large inputs.