Skip to content

Re-benchmark num_digits_fast against num_digits_faster after the boundary fix #15763

Open

Description

@jgohel9902

Feature description

Feature description

PR #15761 (fixing #15754) makes num_digits_fast in maths/number_of_digits.py exact by adding a correction step after math.log10. The correction computes 10 ** (digits - 1) and 10**digits, which are big-integer powers and add cost for large inputs.

In a quick local timing on 2**100 (200,000 calls with timeit), the corrected approach took about 0.148 s, versus about 0.035 s for len(str(abs(n))). So the function named "fast" may no longer be the fastest of the three.

Proposed follow-up, once #15761 is merged:

  1. Re-run benchmark() in the file and record the results for all three functions.
  2. If num_digits_fast is no longer faster, update its docstring to say it is a log10-based approach, or rename or reorder the functions so the names match the results.
  3. Add a note on the int-to-string length limit: len(str(n)) raises ValueError for ints above about 4300 digits by default in Python 3.11+, while the log10 approach avoids it. That is a reason to keep it.

I'm happy to open a PR for the docstring and benchmark update after #15761 lands.

Activity

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

    enhancementThis PR modified some existing files

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions