Both versions compile to nearly identical bytecode, so the else
isn’t what speeds things up. The small timing difference comes from how CPython handles sets internally and from random cache or branch-prediction effects at the C level, not from Python’s control flow. In short, it’s just measurement noise — not a real optimization. Use whichever version reads clearer.