Skip to content

Speed up int.__round__ for large negative ndigits #132474

@devdanzin

Description

@devdanzin

Feature or enhancement

Proposal:

Rounding an int by a large positive ndigits is instantaneous, while a by a large negative number takes ages, only to return zero:

>>> large_number = 2**31
>>> large_negative_number = -2**31
>>> d = 17000
>>> d.__round__(large_number)  # Instant result, returns d
17000
>>> d.__round__(large_negative_number)  # Takes forever, calculating divmod(d, int(pow(10, large_negative_number)))

It could be almost as fast as the positive ndigits case by checking:

int(math.log10(d)) < abs(large_negative_number)

And returning zero if it's True.

A similar optimization is possible for Fraction: #132472

I realize there may not be a real use case for these enhancements.

cc @JelleZijlstra

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

It was very briefly discussed on Discord.

Linked PRs

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