Stack Samples

Start Timer

0:00:00

Upvote
0
Downvote
Save question
View comments

We’re given the output of a sampling profiler: a list of (timestamp, stack) samples in time order, where each stack lists the active functions from outermost to innermost.

Write a function called to_trace_events that returns one (name, start, end) event for every function call, sorted by start time with outer calls first.

Note: A call starts at the first sample it appears in and ends at the first sample where it is gone. The last sample ends anything still open.

Input:

samples = [
    (0, ["main"]),
    (1, ["main", "load"]),
    (2, ["main", "load", "parse"]),
    (3, ["main", "load"]),
    (4, ["main", "save"]),
    (5, ["main"]),
]

Output:

def to_trace_events(samples) -> [
    ("main", 0, 5),
    ("load", 1, 4),
    ("parse", 2, 3),
    ("save", 4, 5),
]
.
.
.
.
.


Comments

Loading comments