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
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.
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.
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
- Measure first. Use
time.perf_counter()for quick checks,timeitfor small snippets andcProfileto find hot functions. Guessing is usually wrong. - 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 listis O(n),x in setandd[key]are O(1) on average,list.insert(0, x)andlist.pop(0)are O(n) (usecollections.dequefor queues). - Use built-ins and the standard library.
sum,min,max,sorted,any,all,str.join,str.split,collections.Counter,itertoolsandbisectrun their loops in C. - Prefer comprehensions to
appendloops when building a collection; they avoid repeated method lookups. - Build strings with
"".join(parts), not by+=in a long loop. - Stream with generators when you do not need all results at once (large files, pipelines). They keep memory flat.
- Cache pure functions with
functools.cacheorlru_cache. Arguments must be hashable. - 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. - Use the right tool for numbers. For large numeric arrays use NumPy; vectorized operations are typically orders of magnitude faster than Python loops.
- Use parallelism for the right problem. Threads help with waiting on I/O (
asyncio,threading); CPU-bound work needsmultiprocessingorconcurrent.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.getsizeofreports only an object's own size, not its contents.