Profiling and caching
Measure work and cache only results whose validity is understood.
On this page
The concept
Performance work should begin with a reproducible workload. perf_counter measures elapsed duration; cProfile reports where calls and time accumulate. From this lesson folder, python3 -m cProfile -s cumulative lesson.py demonstrates the profiling command without adding timing prose to the executable example.
lru_cache remembers results by hashable arguments and evicts older entries after its limit. It fits deterministic functions such as this text normalization. Cached results derived from files or mutable external state need an invalidation policy. A cache changes memory use and freshness guarantees, not just speed.
Worked example
from functools import lru_cache
@lru_cache(maxsize=128)
def normalize(text):
return " ".join(text.casefold().split())
print(normalize(" Python CODE "))
print(normalize(" Python CODE "))
print(normalize.cache_info())Trace the result
- The first normalization call misses the cache and computes a result.
- The second call with identical input reuses the cached result.
- cache_info reports one miss and one hit for these calls.
python code
python code
CacheInfo(hits=1, misses=1, maxsize=128, currsize=1)Line-by-line walkthrough
Line 1: from functools import lru_cache
Import lru_cache from functools. This supplies function wrappers and caching. The caller can now use the imported names directly.
Lines 5–6: def normalize(text):
Define the function normalize with input names text. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.
Line 6: return " ".join(text.casefold().split())
Evaluate ' '.join(text.casefold().split()) and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.
Line 9: print(normalize(" Python CODE "))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 10: print(normalize(" Python CODE "))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 11: print(normalize.cache_info())
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
An unbounded cache can grow without limit. A list is not a hashable argument, and stale external data is not fixed by caching it.