Skip to content

Improve performance of range #116477

Closed
Closed

Description

@eendebakpt

Feature or enhancement

Proposal:

The range object is often used in python code. The current implementation is already efficient for integers that fit into a C long, but can be improved. The most performance critical use case is probably with a single-argument in a for loop like:

for x in range(N):
	...

For that case we can avoid several conversions of Python integers to C long and a construction of a new Python integer by adding a fast path for the single argument case (e.g. start=0 and step=1).

Alternative ideas:

  • Modify the interpreter to change in range(...) in a direct construction of the iterable (skipping the range object). This would be hard to implement backwards compatible, since range could have been changed by a user to a custom implementation.
  • range is read-only, so we could construct an iterable with a reference to the range object to save memory. Since the current iterable (_PyRangeIterObject) is already very small, it seems unlikely this will gain something.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added a commit that references this issue on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions