What changed between two samples
Back to the profiler. A sample is a pair (time, stack). The stack lists the calls outermost first, the way extract_stack printed them. Here are two samples in a row:
(30, ["main", "load", "parse"])
(40, ["main", "save"])
What happened in between?
Frames shared from the bottom kept running, and the rest ended or started
Compare the stacks from the bottom, depth by depth:
main is shared: the same call. load and parse ended, in red, and save started, in green.
The frames the two stacks share from the bottom are calls that kept running. From the first difference up, every frame of the old stack is a call that ended. Every frame of the new one is a call that started.
The order is forced too. parse had to return before load could. load had to return before main could call save. So the stack went through these steps between the two samples:
The ends go innermost first, and the starts go outermost first:
[(40, 'end', 'parse'), (40, 'end', 'load'), (40, 'start', 'save')]
Comparing two stacks costs O(d) for depth d. So turning s samples into events costs O(s·d), and you only ever hold the previous stack.
In the editor, write shared(before, after): how many frames two stacks share from the bottom. The demo line compares the samples at 30 and 40, which share only main.