Skip to content
Tutorials Center
My learning

Topics

Lesson 94 of 149 ยท Beyond the Basics

Measuring and Improving Performance

Make Python code faster the right way. Time code with timeit, find hotspots with cProfile, pick the right data structures, save memory with generators, and cache results.

Sooner or later a program feels slow: a report takes a minute, a web page takes three seconds, a script chews through all your memory. The tempting reaction is to start "optimizing" whatever code looks inefficient. Usually that wastes time, because programmers are famously bad at guessing where the time really goes.

This lesson teaches the reliable approach: measure first, find the few lines that matter, fix them with the right tool, then measure again. You'll use timeit and cProfile from the standard library, see why choosing the right data structure beats every micro-trick, and collect a short list of speed-ups that are worth knowing.

Timings on this page are browser timings. The editors run Python compiled to WebAssembly, which is usually a few times slower than Python installed on your computer. The relative differences between approaches are what matter, and they hold up on both.

The golden rules

Before any code, three rules that save hours:

  1. Make it work, then make it fast. Correct, readable code comes first. A fast program with wrong answers is worthless.
  2. Measure, don't guess. Profile the real program with realistic data and find the hotspot. Most programs spend nearly all their time in a small fraction of their code.
  3. Fix the biggest problem first. A better algorithm can turn minutes into milliseconds. Shaving a few percent off a line that runs once changes nothing.

Timing small snippets with timeit

For a quick "how long did that take", time.perf_counter() gives a high-resolution clock: read it before and after, and subtract. But a single measurement of a fast operation is noisy. The computer might be busy with something else for a moment.

The timeit module fixes that. It runs a snippet many times (number), and timeit.repeat() does that whole measurement several times (repeat). Take the minimum of the repeats: it's the run with the least interference from the rest of the system.

Python Comparing four ways to build a list

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.

Run it a few times. The comprehension usually beats the loop, and map(lambda ...) is usually the slowest, because calling a Python function for each item costs more than running the expression inline. But notice how small the differences are. Rewriting loops into comprehensions rarely rescues a slow program; it's a nice side effect of writing idiomatic code (see List Comprehensions).

On your own computer, timeit also works straight from a terminal, which is handy for quick experiments:

TEXT
python -m timeit -s "data = list(range(1000))" "sum(data)"
20000 loops, best of 5: 11.2 usec per loop

Finding hotspots with cProfile

timeit compares snippets you already suspect. To find out where a whole program spends its time, use a profiler. cProfile records every function call: how many times it was called and how long it took.

Python Profiling a program

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.

How to read the table:

  • ncalls: how many times the function was called.
  • tottime: time spent inside the function itself, not counting functions it called.
  • cumtime: total time including everything it called.

is_prime takes almost all the time, while word_score barely registers, even though it's called 2,000 times. So that's the only function worth improving. Here the fix is mathematical: a number that has a divisor has one no larger than its square root, so range(2, math.isqrt(n) + 1) checks far fewer numbers. Make that change, run it again, and watch is_prime shrink.

To profile a whole script on your computer, run python -m cProfile -s cumulative my_script.py. For a visual view, tools such as snakeviz draw the profile as a chart, and sampling profilers such as py-spy can even attach to a program that is already running.

Choose the right data structure

The biggest wins rarely come from clever tricks. They come from using a structure whose operations are fast for what you do most. Two classic examples:

  • Checking item in some_list looks at items one by one, so it gets slower as the list grows. A set or dictionary finds an item in roughly constant time, no matter how big it is.
  • list.pop(0) removes the first item and shifts every other item one place left. A collections.deque (see collections) removes from either end without shifting anything.
Python Lists vs sets vs deques

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.

The set is thousands of times faster here, and the gap grows with the data. This is what "Big-O" notation describes: list membership is O(n) (time grows with the number of items), set membership is O(1) on average (time stays flat). You'll study this properly in Searching and Sorting Algorithms.

A quick reference for the operations you use most:

Operation list set / dict deque
x in container slow, O(n) fast, O(1) slow, O(n)
Add or remove at the end fast fast (add) fast
Add or remove at the front slow, O(n) n/a fast
Access by index c[i] fast n/a (dict: by key, fast) slow in the middle

A common pattern: if you check membership inside a loop, convert the list to a set once, before the loop.

Save memory with generators

Speed is not the only resource. A list holds every item in memory at once, while a generator produces items one at a time and forgets each one after use:

Python A list vs a generator

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.

The list needs megabytes; the generator needs a couple of hundred bytes, whatever the range. (And getsizeof only counts the list's array of references, not the integer objects themselves, so the real gap is even bigger. See Memory Management.) When you only loop over data once, for example when summing a file line by line, a generator lets you process files much bigger than your memory.

Cache repeated work

If a function is called again and again with the same arguments, and always returns the same result for them, you can remember the answers. functools.lru_cache (and its unlimited sibling functools.cache, covered in itertools and functools) does it with one decorator:

Python Caching a recursive function

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.

The uncached version recomputes the same small grids hundreds of thousands of times. The cached one computes each grid once (143 misses) and answers the rest from memory. Caching only works for functions whose arguments are hashable and whose results don't depend on anything else, such as the current time or a file's contents. Give maxsize a number when the cache could otherwise grow without limit.

A checklist of common speed-ups

Once the profiler points at a hotspot, these fixes come up again and again:

  • Use built-ins. sum(), min(), max(), sorted(), any(), all() and str.join() run in C and beat hand-written loops.
  • Build strings with join(), not += in a loop: ", ".join(parts) copies the text once.
  • Move work out of loops. Compile a regular expression, look up a setting or convert a list to a set once, before the loop.
  • Avoid repeated lookups in very hot loops: storing append = results.append in a local name skips an attribute lookup per call. Use this only where the profiler says it matters, since it hurts readability.
  • Read files in chunks or line by line instead of loading everything, and use csv or json rather than hand-made parsers.
  • Batch I/O. One database query that returns 1,000 rows beats 1,000 queries that return one row each.
  • Overlap waiting. If the time goes into network calls, use threads or asyncio so the waits overlap.

When pure Python isn't enough

Sometimes the algorithm is already right and the code is still too slow, because it does millions of small operations in Python. Then the usual next steps are:

  • NumPy for numeric arrays: one vectorized expression replaces a whole loop and runs in C (see NumPy Basics).
  • Processes to spread CPU-heavy work across cores with ProcessPoolExecutor (see Threads, Processes and concurrent.futures).
  • A faster interpreter. PyPy runs many pure-Python programs several times faster. CPython itself gets faster with each release, and recent versions include an experimental JIT compiler that you can enable at build time.
  • Compiled extensions written with Cython, or in C or Rust, for the hottest few functions.

Each of these adds complexity. Reach for them only after measuring, and keep the profile from before and after to prove the change was worth it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A script is slow. What should you do first?

    Most programs spend most of their time in a small part of the code. Measuring with a profiler tells you which part, so your effort goes where it counts.
  2. 2. Why should you compare the minimum of the times returned by timeit.repeat()?

    Background activity can only add time. The fastest run is the closest to the snippet's true cost.
  3. 3. In a cProfile report, what does cumtime measure?

    cumtime is cumulative time including callees. tottime excludes them, and ncalls counts calls.
  4. 4. You check if code in valid_codes inside a loop over a million records, and valid_codes is a list of 50,000 items. What is the best fix?

    Membership in a list scans the items one by one. A set answers in roughly constant time, and converting it once costs little.
  5. 5. When is functools.lru_cache a safe choice?

    A cache returns a stored answer for the same arguments. That's only correct when the function is deterministic and has no side effects that must happen on every call.

Summary

  • Make code correct and readable first; optimize only what measurements prove is slow.
  • timeit.repeat() times small snippets reliably; compare the minimum runs.
  • cProfile with pstats shows which functions use the most time; sort by cumulative to find hotspots.
  • Choosing the right data structure (sets for membership, deques for queues) beats any micro-optimization.
  • Generators keep memory flat; lru_cache skips repeated work for pure functions.
  • Built-ins, join(), batching I/O and moving work out of loops are the everyday speed-ups; NumPy, processes, PyPy and compiled extensions are the heavy tools.

Next: Memory Management, where you'll see how Python creates, shares and frees objects behind the scenes.