Skip to content

minor inefficency in the statistics module #154947

Closed as not planned
Closed as not planned

Description

@lbaumbauer

Bug report

Bug description:

j = i * m // n # rescale i to m/n

in the quantiles computation 'i * m' should be reused not recomputed ( #150326) changes not added here)

            i_m = i * m
            j =  i_m // n                               # rescale i to m/n
            j = 1 if j < 1 else ld-1 if j > ld-1 else j  # clamp to 1 .. ld-1
            delta = i_m - j*n 

CPython versions tested on:

3.13

Operating systems tested on:

No response

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    performancePerformance or resource usage
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Jul 30, 2026
  2. picnixz commented on Jul 30, 2026

    @picnixz
    Member

    If you believe there is a real improvement this should be backed by benchmarks. I do not think this matters though.

  3. added
    pendingThe issue will be closed if no feedback is provided
    on Jul 30, 2026
  4. lbaumbauer commented on Jul 30, 2026

    @lbaumbauer
    Author

    lol

    `from timeit import timeit

    a = 1.23456789

    def test_a(ld, i, m):
    n = 100
    i_m = i * m
    j = i_m // n # rescale i to m/n
    j = 1 if j < 1 else ld-1 if j > ld-1 else j # clamp to 1 .. ld-1
    delta = i_m - j*n

    def test_b(ld, i, m):
    n = 100
    j = i * m // n # rescale i to m/n
    j = 1 if j < 1 else ld-1 if j > ld-1 else j # clamp to 1 .. ld-1
    delta = i * m - j*n

    _ld = 234
    _i = 3
    _m = _ld + 1

    x = timeit(lambda : test_a(_ld, _i, _m), number=10_000_000)
    y = timeit(lambda : test_b(_ld, _i, _m), number=10_000_000)

    print(f"{x / y}")
    `

    0.925

  5. picnixz commented on Jul 31, 2026

    @picnixz
    Member

    This benchmark is neither correct nor relevant; I am closing this. Use pyperf for more robust checks and benchmark the entire function as well instead of just thjs arithmetic part. Timeit does not givr any information (and use PGO/LTO builds).

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

    pendingThe issue will be closed if no feedback is providedperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions