◐ Shell
clean mode source ↗

bpo-42222: Modernize integer test/conversion in randrange() by rhettinger · Pull Request #23064 · python/cpython

Moves the int(x) == x test and conversion into the C code for operator.index().

Provides a 7% speed up:

$ pytime -s 'from random import randrange' 'randrange(15)'  # baseline
500000 loops, best of 11: 540 nsec per loop
$ pytime -s 'from random import randrange' 'randrange(15)'   # new versio
500000 loops, best of 11: 502 nsec per loop

It should become even faster when we get a zero-cost try.

It's odd that non-integers raise a ValueError. I think that should have been TypeError.

https://bugs.python.org/issue42222