Optimization

Optimization is about making code run faster or use less memory. In practice, you should first make your code correct and readable, and then use profiling tools to identify real bottlenecks before optimizing the critical paths.

While optimizing your code, you can apply the following best practices:

  • Avoid premature optimization. Don’t pursue apparent performance gains without first identifying real problems. Focus on readability and correctness first, then optimize only where profiling shows a real bottleneck.
  • Profile before you optimize. Use tools like timeit, cProfile, or other profilers to measure where time is spent. Optimize the hot paths and measure again to confirm the improvement. This practice helps ensure you put your optimization efforts in the correct place.
  • Prefer algorithmic improvements over clever tweaks. Switching to a better algorithm or a more suitable data structure often yields larger performance gains than small syntactic changes or clever one-liners.

To see how optimization can improve performance, here’s an example where choosing the right data structure can make a dramatic difference. Imagine you need to filter a large stream of events by whether the .user_id value is in an allowlist:

🔴 Avoid this:

Python
def filter_allowed_events(events: list, allowed_user_ids: list) -> list:
    allowed_events = []

    for event in events:
        if event.user_id in allowed_user_ids:
            allowed_events.append(event)

    return allowed_events

This function works, but membership checks on a list are linear and can impact the code’s speed when done repeatedly. If allowed_user_ids and events are large, this loop can become a hot path.

Favor this (optimized):

Python
def filter_allowed_events(events: list, allowed_user_ids: list) -> list:
    allowed_set = set(allowed_user_ids)
    return [event for event in events if event.user_id in allowed_set]

By converting allowed_user_ids to a set once, you make each membership test much cheaper on average. This kind of change often yields considerable speed wins because it reduces the algorithmic cost of the hottest operation.

Tutorial

Profiling in Python: How to Find Performance Bottlenecks

In this tutorial, you'll learn how to profile your Python programs using numerous tools available in the standard library, third-party libraries, as well as a powerful tool foreign to Python. Along the way, you'll learn what profiling is and cover a few related concepts.

intermediate tools

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Jan. 8, 2026 • Reviewed by Martin Breuss