Re: Better compatibility of the Python scientific/data stack with fast Python interpreters

Stefan Krah <[email protected]>
Newsgroups gmane.comp.python.numeric.general
Message-ID <[email protected]>
On Wed, May 07, 2025 at 03:52:33PM +0200, PIERRE AUGIER via NumPy-Discussion wrote:
> > "If the goal is to run psycopg3 fast in PyPy, there are at least three paths:
> > 
> >    use a cffi backend: this is likely to be the fastest one
> >    (in case psycopg uses Cython and doesn't call directly any CPython C API): use
> >    the Cython HPy backend, once it's ready
> >    (in case psycopg uses the CPython C API directly): rewrite it to use HPy
> >    instead."
[cut]

> Could you explain a bit more? Or provide a link? CFFI with PyPy? or CFFI with CPython?

Yes: _if_ the above 2022 quote from the psycopg3 issue is still valid,
and my benchmarks show that the CPython C-API is faster than the PyPy
CFFI, then (according to the quote), HPy is slower than PyPy CFFI and
therefore much slower than CPython C-API.

All of that under the assumption that there are many API calls.


Stefan Krah


> Is there a proper reproducible benchmark?

Here are the benchmarks for _decimal:

bench.py
========================================================================
import time
import platform

if platform.python_implementation() == "CPython":
    import _decimal as C
else: # PyPy
    import __decimal as C

def pi(D):
    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


print("\n# ======================================================================")
print("#                   Calculating pi, 10000 iterations")
print("# ======================================================================\n")

prec = 9
print("\nPrecision: %d decimal digits\n" % prec)
start = time.time()
C.getcontext().prec = prec
for i in range(10000):
    x = pi(C.Decimal)
print("result: %s" % str(x))
print("time: %fs\n" % (time.time()-start))
========================================================================


### NOTE: only execute git clean -xdf in a throwaway repository! ###
git clone https://github.com/python/cpython
cd cpython
git checkout v3.9.0
./configure && make -j32
cd ..
./cpython/python bench.py
[cut]
time: 0.169412s


cd cpython
git clean -xdf
git checkout v3.13.0
./configure && make -j32
cd ..
./cpython/python bench.py
[cut]
time: 0.238405s  # Massive slowdown compared to 3.9.


wget https://downloads.python.org/pypy/pypy3.11-v7.3.19-linux64.tar.bz2
tar xvf pypy3.11-v7.3.19-linux64.tar.bz2
cd pypy3.11-v7.3.19-linux64/lib/pypy3.11/
../../bin/pypy _decimal_build.py  # Builds _decimal_cffi using libmpdec.
cd ../../..
./pypy3.11-v7.3.19-linux64/bin/pypy bench.py
[cut]
time: 2.433634s



_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.