-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_benchmarking.py
More file actions
34 lines (26 loc) · 2.81 KB
/
function_benchmarking.py
File metadata and controls
34 lines (26 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import timeit
from mpmath import mp, mpf
import pandas as pd
LIST_OF_FUNCTION_STRINGS = [' acosh(x)', ' acosh(x**2)', ' acos(x)', ' acot(x)', ' acot(x**2)', ' acoth(x)', ' acsc(x)', ' acsc(x**2)', ' acsch(x)', ' agm(x, x**2)', ' airyai(x)', ' airybi(1/x)', ' asec(x)', ' asech(x)', ' asin(x)', ' asinh(x)', ' atan(x)', ' atan2(x, x)', ' atanh(x)', ' barnesg(1/x)', ' besseli(x, 1/x)', ' besselk(x, 1/x)', ' bessely(x, 1/x)', ' beta(x, x)', ' cbrt(x**0.1)', ' chebyt(x, x**2)', ' chebyu(x, x**2)', ' chi(x)', ' clcos(x, x)', ' clsin(x, x)', ' cos(x)', ' cos(x**2)', ' cosh(x)', ' cot(x)', ' csch(x)', ' degrees(x)', ' ellipk(x)', ' elliprc(x, x)', 'ellipf(x,-x)', 'ellipe(x,-x)', ' elliprg(x, x, x)', ' erfc(x)', ' exp(x)', ' exp(x**2)', ' expm1(x)', ' gamma(x)', ' hankel1(x, 2)', ' hankel2(x, 2)', ' harmonic(x)', ' harmonic(x**2)', ' hermite(x, x)', ' hypot(x,x**2)', ' jacobi(x, x, x, x)', ' laguerre(x, 1/x, 1/x, maxterms=10**10)', ' lambertw(x)', ' lambertw(x**2)', ' legendre(x, x)', ' ln(x)', ' ln(x**2)', ' log(x)', ' log(x**2)', ' log10(x)', ' loggamma(x**2)', ' powm1(x,x)', ' power(x, 3)', ' power(x, 4)', ' power(x, 5)', ' power(x, 6)', ' power(x, 7)', ' power(x, 8)', ' power(x, 9)', ' power(x, 10)', ' power(x, 11)', ' power(x, 12)', ' psi(x, x**2)', ' radians(x)', ' riemannr(x)', ' root(x,3)', ' root(x,4)', ' root(x,5)', ' root(x,6)', ' root(x,7)', ' root(x,8)', ' root(x,9)', ' root(x,10)', ' root(x,11)', ' root(x,12)', ' scorerhi(x)', ' sec(x)', ' sech(x)', ' shi(x**2)', ' shi(x)', ' si(x**2)', ' si(x)', ' siegeltheta(x)', ' sin(x)', ' sin(x**2)', ' sinc(x)', ' sinh(x)' , ' tanh(x)']
def benchmark_mpmath_functions( precisions, num_iterations):
results = []
for func_name in LIST_OF_FUNCTION_STRINGS:
for precision in precisions:
print(f"Current function: {func_name} | Precision: {precision}")
mp.dps = precision
setup_code = f"from mpmath import mp, mpf; mp.dps = {precision}; x = mpf(1) / mpf(10**{10})"
test_code = f"mp.{func_name.strip()}"
execution_time = timeit.timeit(stmt=test_code, setup=setup_code, number=num_iterations)
computations_per_second = num_iterations / execution_time
results.append([func_name.strip(), precision, computations_per_second])
df = pd.DataFrame(results, columns=["Function Name", "Precision", "Computations per Second"])
return df
# Set the precision levels to benchmark
precisions = [16, 32, 64, 128, 256, 512] # 1024, 2056, 4096, 8192]
# Set the number of iterations for each benchmark
num_iterations = 5
# Run the benchmarks
results_df = benchmark_mpmath_functions( precisions, num_iterations)
# Print the results DataFrame
print(results_df)
results_df.to_csv('mpmath_benchmark_results.csv', index=False)