lifolofi
lifolofi

Reputation: 129

Sympy on PyPy - Sometimes 6x faster, sometimes 4x slower

Here, pypy is slower in calculating, whether a given number is prime:

C:\Users\User>python -m timeit -n10 -s"from sympy import isprime" "isprime(2**521-1)"
10 loops, best of 3: 25.9 msec per loop

C:\Users\User>pypy -m timeit -n10 -s"from sympy import isprime" "isprime(2**521-1)"
10 loops, best of 3: 97.9 msec per loop

Here, pypy is faster in creating a list of primes (from 1 to 1000000):

C:\Users\User>pypy -m timeit -n10 -s"from sympy import sieve" "primes = list(sieve.primerange(1, 10**6))"
10 loops, best of 3: 2.12 msec per loop

C:\Users\User>python -m timeit -n10 -s"from sympy import sieve" "primes = list(sieve.primerange(1, 10**6))"
10 loops, best of 3: 11.9 msec per loop

Very surprising, hard to understand.

“If you want your code to run faster, you should probably just use PyPy.” — Guido van Rossum (creator of Python)

Am I missing something?

Upvotes: 1

Views: 500

Answers (1)

Veedrac
Veedrac

Reputation: 60147

isprime has a fast path for when gmpy is installed. gmpy has bindings to a highly optimized C library, and is probably only installed on CPython.

Upvotes: 5

Related Questions