divmod on long numbers is slower in PyPy than in CPython
Created originally on Bitbucket by sargeborsch (Sarge Borsch)
(AFAIK PyPy is supposed to be good at optimizing exactly these kinds of computations, so:)
The code is below. The computational part executes in ~330 ms with CPython 3.6.4 (on average) but it takes ~1979ms under PyPy3 (detailed version below)
#!python
def measure(fun): # used to measure run time of the interesting part
from time import time
t = time()
result = fun()
elapsed_ms = int(10 ** 3 * (time() - t))
print(str(elapsed_ms) + ' ms')
return result
def sum_digits(n):
s = 0
while n:
n, r = divmod(n, 10)
s += r
return s
print(measure(lambda: sum_digits(11 ** 20000)))
PyPy3 detailed version:
$ pypy3 --version
Python 3.5.3 (09f9160b643e3f02ccb8c843b2fbb4e5cbf54082, Dec 26 2017, 09:50:12)
[PyPy 5.10.0 with GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
OS is macOS 10.12.6.
both PyPy3 and CPython3 were installed from homebrew, with default options.
For most other 'number-crunching' programs PyPy3 gives ~2-6 times speedup on my machine (as expected), so it's probably not a broken build.
I tried to further isolate the cause but to no avail so far. I will probably try again in some time. Fortunately the code is not too big already.
Now, should I also test this on some previous PyPy3/2 versions?