Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Practical Pages

Python performance tips

Concept page with practical Python performance advice: measure first, choose the right data structure, avoid repeated work, use built-ins, generators and caching.

Most Python programs are fast enough. When one is not, the biggest wins almost always come from three things, in this order: measure where the time goes, pick a better algorithm or data structure, and let built-in, C-implemented functions do the looping. Micro-tweaks such as shaving an attribute lookup rarely matter and make code harder to read.

This page collects advice that holds up in practice. Timings depend on your machine and Python build, so the examples below show what is faster in terms of work done (counts, sizes), not stopwatch numbers.

Try it

Python Choose the right data structure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Build strings and lists the efficient way

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Generators and caching avoid repeated work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Advice that pays off

  1. Measure first. Use time.perf_counter() for quick checks, timeit for small snippets and cProfile to find hot functions. Guessing is usually wrong.
  2. Improve the algorithm. Replacing a nested loop with a dict or set lookup can turn minutes into milliseconds. Know the cost of common operations: x in list is O(n), x in set and d[key] are O(1) on average, list.insert(0, x) and list.pop(0) are O(n) (use collections.deque for queues).
  3. Use built-ins and the standard library. sum, min, max, sorted, any, all, str.join, str.split, collections.Counter, itertools and bisect run their loops in C.
  4. Prefer comprehensions to append loops when building a collection; they avoid repeated method lookups.
  5. Build strings with "".join(parts), not by += in a long loop.
  6. Stream with generators when you do not need all results at once (large files, pipelines). They keep memory flat.
  7. Cache pure functions with functools.cache or lru_cache. Arguments must be hashable.
  8. Move work out of loops: compute invariants once, compile regular expressions once (re.compile), and bind frequently used global functions to locals only in measured hot paths.
  9. Use the right tool for numbers. For large numeric arrays use NumPy; vectorized operations are typically orders of magnitude faster than Python loops.
  10. Use parallelism for the right problem. Threads help with waiting on I/O (asyncio, threading); CPU-bound work needs multiprocessing or concurrent.futures.ProcessPoolExecutor. The classic GIL still serializes Python bytecode in the default build, though Python 3.13+ offers an optional free-threaded build.

Usage notes

  • Readable code first. A 5% gain is not worth a clever one-liner nobody can maintain.
  • Slicing and copying large lists is O(n); avoid doing it inside loops when an index will do.
  • __slots__, local variable aliasing and similar tricks give small gains; apply only after profiling.
  • Newer Python versions are faster on their own (3.11 and later notably so), so upgrading is often the cheapest optimization.
  • Memory counts too: sys.getsizeof reports only an object's own size, not its contents.