Skip to content

bpo-42222: Modernize integer test/conversion in randrange()#23064

Merged
rhettinger merged 9 commits into
python:masterfrom
rhettinger:fast_randrange
Dec 28, 2020
Merged

bpo-42222: Modernize integer test/conversion in randrange()#23064
rhettinger merged 9 commits into
python:masterfrom
rhettinger:fast_randrange

Conversation

@rhettinger
Copy link
Copy Markdown
Contributor

@rhettinger rhettinger commented Oct 31, 2020

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