Skip to content

Latest commit

 

History

History
74 lines (54 loc) · 2.25 KB

File metadata and controls

74 lines (54 loc) · 2.25 KB

Example: seeing a real diff

workload.py ships two implementations of the same function — count_duplicate_pairs_slow (O(n²), with a wasteful per-iteration list copy) and count_duplicate_pairs_fast (O(n) via a frequency table). run_workload() dispatches between them based on the PROFILE_DIFF_SLOW environment variable, so you can profile "two branches" from a single checkout.

Run everything from the repository root (so examples.workload imports and paths normalise the same way both times):

1. Profile the slow "base" and the fast "head"

# Base = slow O(n^2) implementation
PROFILE_DIFF_SLOW=1 python -m profile_diff run \
    --callable examples.workload:run_workload --label base --out base.json

# Head = fast O(n) implementation
PROFILE_DIFF_SLOW=0 python -m profile_diff run \
    --callable examples.workload:run_workload --label head --out head.json

2. Diff them

python -m profile_diff compare base.json head.json --threshold-pct 5

You will see an improvement — the slow hotspot disappears and total cumtime collapses:

## Performance profile diff

**Runtime (total cumtime):** 59.5 ms → 0.8 ms (−58.7 ms ▼)

### Top improvements ▼ (faster)

| Function | Base | Head | Δ | Δ% |
|---|---:|---:|---:|---:|
| `examples/workload.py:45(run_workload)` | 59.5 ms | 0.1 ms | −59.4 ms | −99.8% |

### Removed hotspots

| Function | cumtime |
|---|---:|
| `examples/workload.py:24(count_duplicate_pairs_slow)` | 59.5 ms |

3. See a regression (and a failing exit code)

Swap the labels so the slow implementation is the head, and add --fail-on-regression:

PROFILE_DIFF_SLOW=0 python -m profile_diff run \
    --callable examples.workload:run_workload --label base --out base.json
PROFILE_DIFF_SLOW=1 python -m profile_diff run \
    --callable examples.workload:run_workload --label head --out head.json

python -m profile_diff compare base.json head.json \
    --fail-on-regression --format text
echo "exit code: $?"   # -> 2
Top regressions (slower):
  examples/workload.py:45(run_workload): 0.1 ms -> 55.4 ms (+55.3 ms, +41609.9%)
exit code: 2

The exact millisecond numbers vary by machine; the direction and the hotspot identity are what matter.