← 返回首页
bpo-42222: Modernize integer test/conversion in randrange() by rhettinger · Pull Request #23064 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

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 commented Oct 31, 2020
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

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

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Currently you are on the way to #19112 (which already has tests and documentation and cover corner cases).

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions Bot added the stale Stale PR or inactive for long period of time. label Dec 16, 2020
rhettinger removed the stale Stale PR or inactive for long period of time. label Dec 24, 2020

Copy link
Copy Markdown
Member

What about #19112?

Copy link
Copy Markdown
Contributor Author

I merged in the relevant parts of #19112. The principal differences are:

  • The TypeError won't start being emitted until the after the deprecation period.
  • Improved wording for the doc entries.
  • Tests focus solely on the deprecations.
  • All existing behavior remains intact until after the deprecation.

Copy link
Copy Markdown
Member
  • The TypeError won't start being emitted until the after the deprecation period.

As in #19112 initially. I changed ValueError to TypeError on your request.

  • Improved wording for the doc entries.

I would appreciate any suggestions to change wording.

  • Tests focus solely on the deprecations.

Yes, tests in #19112 cover more cases.

  • All existing behavior remains intact until after the deprecation.

As well as in #19112.

But this PR still has issues. It does not emit warning for randrange(10, 20, 1.0).

rhettinger merged commit a9621bb into python:master Dec 28, 2020

Copy link
Copy Markdown

@rhettinger: Please replace # with GH- in the commit message next time. Thanks!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Footer

© 2026 GitHub, Inc.