pypy / pypy

PyPy is a very fast and compliant implementation of the Python language.
https://pypy.org
Other
1.09k stars 58 forks source link

With --jit off, PyPy's interpreter is much slower than CPython #3674

Open gitlab-importer opened 2 years ago

gitlab-importer commented 2 years ago

In Heptapod by @mattip on Feb 13, 2022, 07:40

Thinking about issue #3667, I was wondering how much slower is our base interpreter than CPython's interpreter. When comparing the code below to CPython, I get these results. Can we do better? The first is startup and import time, the difference between the second and third is that in the inner loop both an __and__ and a __add__ are used. Why should that slow down PyPy even more?

command line time relative to CPython
python3.8 /tmp/simple.py 14ms 1x
pypy3.8-HEAD/bin/pypy --jit off /tmp/simple.py 41ms 2.9x
--- --- ---
python3.8 /tmp/simple.py sumup 264ms 1x
python3.8 /tmp/simple.py sumup 1082ms 4.1x
--- --- ---
python3.8 /tmp/simple.py suminv 325ms 1x
python3.8 /tmp/simple.py suminv 1654ms 5.1x

/tmp/simple.py

bits = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608,
16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824,
2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736,
137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552,
4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664,
140737488355328, 281474976710656, 562949953421312, 1125899906842624,
2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984,
36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744,
576460752303423488, 1152921504606846976, 2305843009213693952,
4611686018427387904, 9223372036854775808]

total = 0

def sumup(bits):
    total = 0
    for b, binv in zip(bits, bits[::-1]):
        total += b
    return total

def suminv(bits):
    total = 0
    for b, binv in zip(bits, bits[::-1]):
        total += b & binv
    return total

if __name__ == '__main__':
    import sys
    if len(sys.argv) < 2:
        pass
    elif sys.argv[1] == 'sumup':
        for i in range(100_000):
            total += sumup(bits)
    elif sys.argv[1] == 'suminv':
        for i in range(100_000):
            total += suminv(bits)
gitlab-importer commented 2 years ago

In Heptapod by @arigo on Feb 13, 2022, 07:45

Why should that slow down PyPy even more?

Unless I'm missing something, in the third case all the computations are involving longs. In the second case almost all iterations of the loop are done with machine integers. Maybe try to remove the last entry in bits?

gitlab-importer commented 2 years ago

In Heptapod by @mattip on Feb 13, 2022, 08:21

The last entries do seem to influence the relative timings. Without the last three values in `bits', the ratio is more consistent

command line time relative to CPython
python3.8 /tmp/simple.py 14ms 1x
pypy3.8-HEAD/bin/pypy --jit off /tmp/simple.py 41ms 2.9x
--- --- ---
python3.8 /tmp/simple.py sumup 256ms 1x
python3.8 /tmp/simple.py sumup 956ms 3.7x
--- --- ---
python3.8 /tmp/simple.py suminv 340ms 1x
python3.8 /tmp/simple.py suminv 1250ms 3.7x