Skip to content

Commit 544e306

Browse files
committed
Add benchmarks for some of the basic operations on some of the basic types.
1 parent 4d1dbab commit 544e306

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

benchmarks/simple_benchmarks.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"""Benchmarks for basic operations of a number of flint types."""
2+
3+
import timeit
4+
from flint import arb, fmpz, fmpq, acb
5+
from utils import run_all_benchmarks, print_benchmark_results
6+
7+
NUM_EXECUTIONS = 10000000
8+
9+
10+
def benchmark_arb_addition(num_executions = NUM_EXECUTIONS):
11+
"""Simple benchmark for adding two arbs."""
12+
a = arb(1, 2)
13+
b = arb.pi()
14+
15+
return timeit.timeit(lambda: a + b, number=num_executions)
16+
17+
18+
def benchmark_arb_multiplication(num_executions = NUM_EXECUTIONS):
19+
"""Simple benchmark for multiplying two arbs."""
20+
a = arb(1, 2)
21+
b = arb.pi()
22+
23+
return timeit.timeit(lambda: a * b, number=num_executions)
24+
25+
26+
def benchmark_arb_contains(num_executions = NUM_EXECUTIONS):
27+
"""Simple benchmark for comparing two arbs."""
28+
a = arb(1, 2)
29+
b = arb.pi()
30+
31+
def to_time():
32+
first = a in b
33+
second = a in a
34+
return first, second
35+
36+
return timeit.timeit(to_time, number=num_executions)
37+
38+
39+
def benchmark_fmpz_addition(num_executions = NUM_EXECUTIONS):
40+
"""Simple benchmark for adding two fmpzs."""
41+
a = fmpz(1)
42+
b = fmpz(6)
43+
44+
return timeit.timeit(lambda: a + b, number=num_executions)
45+
46+
47+
def benchmark_fmpz_multiplication(num_executions = NUM_EXECUTIONS):
48+
"""Simple benchmark for multiplying two fmpzs."""
49+
a = fmpz(1)
50+
b = fmpz(6)
51+
52+
return timeit.timeit(lambda: a * b, number=num_executions)
53+
54+
55+
def benchmark_fmpz_eq(num_executions = NUM_EXECUTIONS):
56+
"""Simple benchmark for equality on two fmpzs."""
57+
a = fmpz(1)
58+
b = fmpz(6)
59+
60+
def to_time():
61+
first = a == b
62+
second = a == a
63+
return first, second
64+
65+
return timeit.timeit(to_time, number=num_executions)
66+
67+
68+
def benchmark_fmpq_addition(num_executions = NUM_EXECUTIONS):
69+
"""Simple benchmark for adding two fmpqs."""
70+
a = fmpq(1, 2)
71+
b = fmpq(15, 7)
72+
73+
return timeit.timeit(lambda: a + b, number=num_executions)
74+
75+
76+
def benchmark_fmpq_multiplication(num_executions = NUM_EXECUTIONS):
77+
"""Simple benchmark for multiplying two fmpqs."""
78+
a = fmpq(1, 2)
79+
b = fmpq(15, 7)
80+
81+
return timeit.timeit(lambda: a * b, number=num_executions)
82+
83+
84+
def benchmark_fmpq_eq(num_executions = NUM_EXECUTIONS):
85+
"""Simple benchmark for equality on two fmpqs."""
86+
a = fmpq(1, 2)
87+
b = fmpq(15, 7)
88+
89+
def to_time():
90+
first = a == b
91+
second = a == a
92+
return first, second
93+
94+
return timeit.timeit(to_time, number=num_executions)
95+
96+
97+
def benchmark_acb_addition(num_executions = NUM_EXECUTIONS):
98+
"""Simple benchmark for adding two acbs."""
99+
a = acb(1 + 3j)
100+
b = acb.pi()
101+
102+
return timeit.timeit(lambda: a + b, number=num_executions)
103+
104+
105+
def benchmark_acb_multiplication(num_executions = NUM_EXECUTIONS):
106+
"""Simple benchmark for multiplying two acbs."""
107+
a = acb(1 + 3j)
108+
b = acb.pi()
109+
110+
return timeit.timeit(lambda: a * b, number=num_executions)
111+
112+
113+
def benchmark_acb_eq(num_executions = NUM_EXECUTIONS):
114+
"""Simple benchmark for equality on two acbs."""
115+
a = acb(1 + 3j)
116+
b = acb.pi()
117+
118+
def to_time():
119+
first = a in b
120+
second = a in a
121+
return first, second
122+
123+
return timeit.timeit(to_time, number=num_executions)
124+
125+
if __name__ == "__main__":
126+
print_benchmark_results(run_all_benchmarks(__name__))

benchmarks/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Utility functions for benchmarking."""
2+
3+
import inspect
4+
import sys
5+
6+
7+
def get_all_benchmarks(modulename):
8+
"""Returns a list of all functions in the current module named 'benchmark_*'."""
9+
return [
10+
obj
11+
for name, obj in inspect.getmembers(sys.modules[modulename])
12+
if inspect.isfunction(obj) and name.startswith("benchmark_")
13+
]
14+
15+
16+
def run_all_benchmarks(modulename):
17+
"""Runs all benchmarks in the current file and returns their times."""
18+
return {
19+
benchmark.__name__[len("benchmark_"):]: benchmark()
20+
for benchmark in get_all_benchmarks(modulename)
21+
}
22+
23+
def print_benchmark_results(results):
24+
"""Pretty-prints a set of benchmark results."""
25+
print(results)
26+
longest_name = max(len(name) for name in results.keys())
27+
for name, time in results.items():
28+
print(f"{name:<{longest_name+2}} | {time:>10.5f}")

0 commit comments

Comments
 (0)