Lesson 1 · Unit 03
Allocation
What values cost, and streaming instead of building lists
What you practise
- What objects cost, garbage collection in outline and its pauses, streaming with generators or iterators against building the whole list
Try a question from this lesson
Pick one
To sum the squares of 1 to 10,000,000, version A builds a list of all the squares and then sums it. Version B adds each square to a running total. What differs?
Correct answer
Memory: A holds ten million numbers at once, B holds one
Why
Both do the same ten million multiplications and additions. A also has to find room for ten million numbers that are needed for a moment and then thrown away. B only ever holds the total.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Lists and streams
A list holds all of its items at once. Memory grows with the number of items.
A stream (a generator or iterator) produces one item at a time, on request. Memory stays flat: one item, plus whatever running answer you keep.
Use a stream when each item can be handled and then forgotten: totals, counts, filters, the largest so far.
The limits: a stream can be read only once, and you cannot index into it or ask for its length.
10 more exercises in the app
- 2× type the answer
- 5× multiple choice
- 1× true or false
- 1× match the pairs
- 1× fill the blanks
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.