We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Memory, Copies and Slow Code
PythonSee when two names share one object, what keeps an object alive, and how to measure memory. Then time and profile slow code before you change it. The track ends with optional reading on the garbage collector, profilers, the JIT and native extensions.
This track uses Python. You can make it your practice mode to browse related content across the site.
Your learning sequence
- 1 When Two Names Share One Object Read
- 2 Not solved yet. Fix a Function That Changes Its Caller's List Practice · Easy
- 3 When to Use is and When to Use == Read
- 4 Not solved yet. Fix a Version Class That Compares Strings with is Practice · Easy
- 5 Intern Repeated Strings, but Compare Them with == Read
- 6 Copy Nested Data with deepcopy Read
- 7 Not solved yet. Deep-Copy Only the Sections You Choose Practice · Medium
- 8 What Keeps an Object Alive Read
- 9 Not solved yet. Write a Context Manager Class Practice · Easy
- 10 Handle Errors and Nesting in a Context Manager Read
- 11 Not solved yet. Fix a Nested Context Manager That Hides Errors Practice · Medium
- 12 How the Garbage Collector Finds Cycles Read
- 13 Not solved yet. Walk an Object Graph with gc.get_referents and a Stack Practice · Medium
- 14 Track Objects with Weak References Read
- 15 Not solved yet. Build a Registry That Does Not Keep Objects Alive Practice · Medium
- 16 What sys.getsizeof Measures Read
- 17 Not solved yet. Add Up Nested Sizes, Counting Each Object Once Practice · Medium
- 18 Find Memory Growth with tracemalloc Read
- 19 Not solved yet. Measure Memory Growth and Always Stop Tracing Practice · Medium
- 20 Measure What slots=True Saves Read
- 21 Use a Set for Lookups and a Deque for Queues Read
- 22 Not solved yet. Remove Duplicates in Order and Keep the Latest Items Practice · Easy
- 23 Build Strings with join and Bytes with bytearray Read
- 24 Not solved yet. Build a Report as Text and UTF-8 Bytes Practice · Medium
- 25 Not solved yet. Fix a Slow Join and a Wasteful Top-k Practice · Medium
- 26 Time Your Code with perf_counter Read
- 27 Time Small Pieces of Code with timeit Read
- 28 Not solved yet. Compare Best Timings and Put the Garbage Collector Back Practice · Medium
- 29 Find Slow Functions with cProfile Read
- 30 Not solved yet. Rank Profiled Functions by Self Time or Cumulative Time Practice · Medium
- 31 Fix the Algorithm Before You Reach for NumPy Read
- 32 Not solved yet. Speed Up a Moving Average with Prefix Sums Practice · Medium
- 33 Check the Exact Python Version When You Debug the Collector Read
- 34 Keep Forked Workers Sharing Memory with gc.freeze Read
- 35 Profile with py-spy, Memray, Scalene and line_profiler Read
- 36 Sample a Running Program with Tachyon in Python 3.15 Read
- 37 Check Whether Your Python Has a JIT, Then Measure Read
- 38 When to Write a C or Rust Extension Read
Check yourself
7 questions · not saved while you're signed outUse these questions to check your understanding and compare explanations. Your score does not affect track completion.
What does this print?
Choose one answer to reveal the explanations. Answers are kept only while this page is open.
def add(x, acc=[]):
acc.append(x)
return acc
print(add(1), add(2), add(3))