I am wondering why mpmath is so much slower than decimal when doing the same operation for the same precision settings.
from decimal import *
from mpmath import *
import timeit
from decimal import Decimal as dc
from mpmath import mpf
import sys
# Set the same precision
getcontext().prec = 15
mp.dps = 15
# A random function which does multiplication using mpmath
def mpf_test():
a = mpf('2202020202002020.21212')
b = mpf('3202020202002020.21212')
c = mpf(0)
for _ in range(10000):
c += (a*b) / (a*b)
# The same function which does the multiplication using decimal
def decimal_test():
a = dc('2202020202002020.21212')
b = dc('3202020202002020.21212')
c = dc(0.0)
for _ in range(10000):
c += (a*b) / (a*b)
# Print results
print(F"Using Decimal: {timeit.timeit(stmt=decimal_test, number=100)}")
print(F"Using mpmath: {timeit.timeit(stmt=mpf_test, number=100)}")
# Check if gmpy2 is used in mpmath
if 'gmpy2' in sys.modules:
print(F"You are using gmpy2")
Output:
Using Decimal: 0.3805640869977651
Using mpmath: 2.961118871004146
You are using gmpy2
The difference is around a factor of 8..
I am using Python3.8, my machine is a new T14s with AMD7 and 32 GB RAM (Dont know if it makes any difference.. )