Skip to content

float/cdecimal: significant slowdown in threaded pi benchmark compared to 3.9 #114922

Closed as not planned

Description

@skrah

Bug description:

The following threaded pi benchmark runs faster on 3.9 than on main. The machine is a six-core i5-8600K with all cores saturated.

import time
import sys
import threading
import _decimal as C
import _pydecimal as P

def pi_float():
    """native float"""
    lasts, t, s, n, na, d, da = 0, 3.0, 3, 1, 0, 0, 24
    while s != lasts:
        lasts = s
        n, na = n+na, na+8
        d, da = d+da, da+32
        t = (t * n) / d
        s += t
    return s

def pi_cdecimal():
    """cdecimal"""
    D = C.Decimal
    lasts, t, s, n, na, d, da = D(0), D(3), D(3), D(1), D(0), D(0), D(24)
    while s != lasts:
        lasts = s
        n, na = n+na, na+8
        d, da = d+da, da+32
        t = (t * n) / d
        s += t
    return s

def test_calc_pi():
    C.getcontext().prec = 9
    for func in [pi_float, pi_cdecimal]:
        start = time.time()
        for i in range(100000):
            x = func()
        print("%s:" % func.__name__.replace("pi_", ""))
        print("result: %s" % str(x))
        print("time: %fs\n" % (time.time()-start))

def test_threaded():
    t = [0] * 6
    for i in range(6):
        t[i] = threading.Thread(target=test_calc_pi)
        t[i].start()

    for i in range(6):
        t[i].join()

if __name__ == "__main__":
    test_threaded()

3.9
===

float: [1.16, 2.15, 2.23, 2.80, 3.28, 4.36]

cdecimal: [2.89, 4.31, 3.02, 4.16, 4.45, 2.84]

real    0m9.365s
user    0m9.440s
sys     0m0.064s


main (--enable-gil)
===================

float: [5.301, 5.40, 6.03, 6.18, 7.19, 7.57]

cdecimal: [8.34, 6.88, 8.47, 8.49, 9.32, 7.21]

real    0m14.296s
user    0m14.472s
sys     0m0.164s


main (--disable-gil)
====================

float: [2.23, 2.90, 3.53, 4.15, 4.11, 4.19]

cdecimal: [6.68, 8.27, 7.42, 7.55, 9.72, 8.61]

real    0m12.250s
user    0m12.356s
sys     0m0.084s

CPython versions tested on:

3.9, CPython main branch

Operating systems tested on:

Linux

Activity

  1. skrah commented on Feb 2, 2024

    @skrah
    ContributorAuthor
  2. skrah commented on Feb 5, 2024

    @skrah
    ContributorAuthor

    Notably, this issue is not present in @colesbury's nogil repository. Perhaps there is some interaction with other features that are being added at the same time.

    nogil-3.12
    ==========
    
    float: [0.28, 0.28. 0.29. 0.28, 0.28. 0.31]
    cdecimal: [1.73, 1.75, 1.82, 1.84, 1.81, 1.82]
    
    real    0m2.205s
    user   0m12.888s
    sys    0m0.004s
    

    Observe that not only _decimal (which currently has module state issues on main) is much faster, but also float.

  3. gpshead commented on Mar 12, 2024

    @gpshead
    Member

    Procedural comment: For all involved on this issue, @skrah was banned from Python spaces per a Conduct Working Group recommendation to the Steering Council in 2020. A configuration oversight on our part in 2020 made that not so on Github. The intended ban of @skrah was reinstated on 2024-02-22. Our apologies to all for the consequences of this mixup (it was the first core dev ban done on our modern systems, we now understand how and why mistakes were made).

    Stefan was informed of this privately via email and re-offered the opportunity to apologize for past actions and to accept our code of conduct going forwards per the original 2020 ban's terms and notification. We have not received a reply.

    I'm posting this here as we view replying in the same forum where issues recently surfaced a reasonable way to notify past, present, and potential future participants of an incident response.


    This says nothing for or against the validity of this issue. A new issue should be opened if people wish to proceed. I'm locking the comments on this one. For information on what the Github concept of a ban means, read the Github org ban docs.

    -- The Python Steering Council, 2024 edition

  4. locked as too heated and limited conversation to collaborators on Mar 12, 2024
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

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions