Chapter 35 · Instrumented reference algorithms
Measure operation effort
Count the work before timing it.
Prerequisite Connect transactions to streams.
Use per-event work, cardinality and output size to qualify Big-O claims.
The question
Two inputs can have the same number of records yet require different amounts of work. This experiment counts comparisons and retained keys for two implementations of duplicate detection.
Big-O describes asymptotic growth under a stated algorithm and input model. Let n be events and k be retained distinct keys. A constant-cost map does O(n) total work. A hash-backed keyed sum has expected O(1) lookup/update per event and O(k) entries, excluding key/value sizes. A linear-list deduplicator may perform O(nk) comparisons. These are different algorithms, not different labels for the same operator.
Predict the result
Keys cycle as i modulo keys for integers i from 0 to n−1.
If you increase distinct keys from 2 to 8 while keeping n=8, which counts should change and which must stay fixed?
Run the experiment
The editor contains the published example. Select Run with the code unchanged.
Python prepares in the background while you read. First use can download about 84 MiB. Your code runs only when you select Run; each run starts with clean experiment state.
A saved draft is open. Select Restore code to see the published example. Recover draft brings back your draft.
Ctrl+Enter or ⌘+Enter runs the code.
Edits are saved in this browser when storage is available.
Technical error details
Your result
Console output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
View the published result
The course build ran the published code to generate this output.
{
"events": 8,
"map_calls": 8,
"hash_aggregate_updates": 8,
"retained_keys": 2,
"linear_dedup_comparisons": 10,
"linear_dedup_retained": 2
}Read the result
Compare events, map_calls, hash_aggregate_updates, retained_keys, and linear_dedup_comparisons. The first three have the same count here. The retained keys and linear search comparisons depend on which keys repeat.
Change the experiment
Replace n=8, keys=2 with n=16, keys=2 in the editor. Select Run again. Then replace n=16, keys=2 with n=16, keys=16 and select Run again. Compare retained_keys and linear_dedup_comparisons across the runs. Explain why the all-distinct case checks every earlier key before retaining the next one.
Runtime, source code and downloads
Runtime and reset details explain what stays ready and what is isolated on each run. Download Python packages the published helpers with the code in your editor.
Browse the sources for Bytewax windows and authored models, Kafka log and group exercises, live Kafka input, checkpoint recovery and processing patterns.
Count bytes and output amplification
A shuffle of n records costs network work proportional to serialized bytes and protocol behavior, not merely n. A sliding window can update several accumulators per event. A many-to-many join can emit z pairs. Name event size, overlap factor, distinct keys and output cardinality before comparing designs.
An implementation can change the bound
A hash lookup’s expected constant time is conditional on its key/hash behavior. A sorted index, linear list and tree have different maintenance and lookup costs. Inspect the selected version’s source or instrument a reference before attributing a complexity claim to a library. The downloadable native harness measures actual Bytewax execution separately.
Work, state and recovery costs
Where used, n counts input events, k counts retained distinct keys, r counts overlapping windows per event and z counts output matches. Bounds assume the stated algorithm. Collecting, sorting and displaying results adds work.
| Question | Mechanism and boundary |
|---|---|
| Work | Counts n map/update calls and linear-list membership comparisons; n and k are named separately. |
| State | O(k) reference entries. Python object/key sizes and hash collision behavior are not represented by entry counts. |
| Movement | The function counts calls and comparisons in memory. |
| Progress | The fixture reports counts after processing all n records. |
| Recovery | Record n and k with the result so another run can reproduce the counts. |
Think it through
Does an O(n) map guarantee bounded memory for the entire stream-processing job?
Show hints
Hint 1
The operator and the complete pipeline have different boundaries.
Hint 2
Output buffering, retained state and downstream speed matter.
Hint 3
Record size is another variable.
Show explanation
No. The function can use constant auxiliary state while downstream queues grow or a sink retains all results. State a boundary and variables: per-event function cost, operator state, queued bytes and retained output. For an unbounded stream, per-event and retained-state costs are often more useful than one total O(n) label.
ReferencesFunction documentation and helper source
References open in a new tab; course helpers are documented separately from library APIs.
Python container operations
The helper instruments explicit algorithms. A language reference describes operations; it does not promise universal constant-time performance for every key or implementation.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
Counters describe the explicit reference implementations. They are not a benchmark or a claim about Bytewax’s internal identity lookup.
Primary reference →