Pyベンチ実測MEASURED, NOT RECALLED

    ↑↓ to select, Enter to open, Esc to close

    例外で分岐 is ~4.11× slower than in で判定

    Catching a KeyError is slower than checking membership first, at this miss rate.

    Result

    Run例外で分岐in で判定Ratio
    Run 147.06711.8843.96×
    Run 249.67812.1014.11×
    Run 349.94912.1394.11×

    Spread over 3 runs: 例外で分岐 CV 2.6% / in で判定 CV 0.9% / Ratio CV 1.7%

    47.07 ms11.88 msRun149.68 ms12.10 msRun249.95 ms12.14 msRun3例外で分岐 (#b9c6d2) / in で判定 (#2f6f4f)

    Conditions

    200,000 lookups against a dict where the key never exists.

    Where this number does not apply

    If most lookups hit, the exception path becomes cheap — the crossover depends on the miss rate.

    Reproduce

    cd bench && python micro_py.py --out ../data/run.json --samples 21