Measure Adjacent Event Gaps with Python itertools.pairwise
A build log records elapsed milliseconds for each event. To find pauses between events, subtract each timestamp from the next one. The middle event belongs to two comparisons: once as the end of a gap and once as the start of the next gap. Python’s itertools.pairwise() expresses this overlap directly.
These examples use Python 3.10 or later and integer elapsed milliseconds from one clock. They calculate gaps in an existing event sequence, preserving its order.
Compare Overlapping Neighbors
Run this standalone example to see the pairs and their differences.
An input of four timestamps produces three pairs. Equal timestamps produce a zero gap; they are not removed. Empty and single-item inputs produce no pairs. The function was added in Python 3.10. Python pairwise documentation
Keep Event Names Beside Each Gap
Save this helper as event_gaps.py. Its input contains (elapsed_ms, event_name) tuples with integer timestamps in nondecreasing order. Record shape and field types are part of the input contract.
Save this as show_gaps.py beside the helper and run python show_gaps.py.
The threshold applies to a single interval between neighboring observations. This example does not attribute the interval to a particular function or prove why the build paused. Missing log records can also make an interval larger.
Preserve Input Order and Detect Regressions
The helper rejects a decreasing timestamp when it reaches that pair. It does not sort the records: sorting could hide a clock reset or change the event sequence you intended to inspect. Use elapsed times from the same clock and keep units consistent. Timestamps from unrelated processes need a separate clock-alignment policy before their differences are meaningful.
Validation happens during iteration. A consumer can receive valid gaps before a later pair raises ValueError. Stopping early also leaves the remaining records unchecked. If a report must validate the entire finite input before publishing any output, consume the helper into a list first and only publish after that succeeds. This stores all gaps in memory.
The helper holds neighboring records as it advances, so it can process a generator without copying the entire input. It still waits for the next record before producing a gap. It cannot report the duration after the final event without an explicit end observation.
Avoid Sharing One Iterator Between zip Arguments
For a list, zip(values, values[1:]) can form overlapping pairs, but the slice copies references and requires a sliceable sequence. Sharing a single iterator between both arguments changes the result. Run this standalone example:
Both zip arguments above advance the same source, taking disjoint pairs. pairwise retains the previous item for overlap. Its consumption also advances the supplied iterator: after exhausting the pairs, that source has no values left. Create a fresh iterator to analyze the records again.
Test Gaps Against Indexed Neighbors
Save this as test_event_gaps.py beside the helper and run python test_event_gaps.py.
| |
The indexed reference checks every neighboring pair, including duplicate timestamps and short inputs. The sum check confirms that the adjacent differences span exactly the first-to-last elapsed time. The final case verifies that a regression raises during consumption after the preceding valid gap.