Skip to content

Commit f3b2c95

Browse files
committed
Use pyperf.
1 parent 544e306 commit f3b2c95

File tree

2 files changed

+164
-122
lines changed

2 files changed

+164
-122
lines changed

benchmarks/simple_benchmarks.py

Lines changed: 164 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,183 @@
1-
"""Benchmarks for basic operations of a number of flint types."""
1+
"""Benchmarks for basic operations of a number of flint types.
22
3-
import timeit
4-
from flint import arb, fmpz, fmpq, acb
5-
from utils import run_all_benchmarks, print_benchmark_results
3+
These benchmarks are written using pyperf, and can be run by
64
7-
NUM_EXECUTIONS = 10000000
5+
python benchmarks/simple_benchmarks.py
86
7+
Since each benchmark is very short ("microbenchmarks"), they may
8+
require an increased number of samples to produce statistically
9+
significant results (via the --values flag).
10+
"""
911

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()
12+
import pyperf
13+
from utils import get_all_benchmarks
1414

15-
return timeit.timeit(lambda: a + b, number=num_executions)
15+
NUM_EXECUTIONS = 10000000
1616

1717

18-
def benchmark_arb_multiplication(num_executions = NUM_EXECUTIONS):
18+
def benchmark_arb_addition(runner: pyperf.Runner):
19+
"""Simple benchmark for adding two arbs."""
20+
runner.timeit(
21+
name="arb addition",
22+
setup=[
23+
"from flint import arb",
24+
"a = arb(1, 2)",
25+
"b = arb.pi()",
26+
],
27+
stmt="a + b"
28+
)
29+
30+
31+
def benchmark_arb_multiplication(runner: pyperf.Runner):
1932
"""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):
33+
runner.timeit(
34+
name="arb multiplication",
35+
setup=[
36+
"from flint import arb",
37+
"a = arb(1, 2)",
38+
"b = arb.pi()",
39+
],
40+
stmt="a * b"
41+
)
42+
43+
44+
def benchmark_arb_contains(runner: pyperf.Runner):
2745
"""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):
46+
runner.timeit(
47+
name="arb contains",
48+
setup=[
49+
"from flint import arb",
50+
"a = arb(1, 2)",
51+
"b = arb.pi()",
52+
],
53+
stmt=[
54+
"a in b",
55+
"b in a",
56+
]
57+
)
58+
59+
60+
def benchmark_fmpz_addition(runner: pyperf.Runner):
4061
"""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):
62+
runner.timeit(
63+
name="fmpz addition",
64+
setup=[
65+
"from flint import fmpz",
66+
"a = fmpz(1)",
67+
"b = fmpz(6)",
68+
],
69+
stmt="a + b"
70+
)
71+
72+
def benchmark_fmpz_multiplication(runner: pyperf.Runner):
4873
"""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):
74+
runner.timeit(
75+
name="fmpz multiplication",
76+
setup=[
77+
"from flint import fmpz",
78+
"a = fmpz(1)",
79+
"b = fmpz(6)",
80+
],
81+
stmt="a * b"
82+
)
83+
84+
85+
def benchmark_fmpz_eq(runner: pyperf.Runner):
5686
"""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):
87+
runner.timeit(
88+
name="fmpz equality",
89+
setup=[
90+
"from flint import fmpz",
91+
"a = fmpz(1)",
92+
"b = fmpz(6)",
93+
],
94+
stmt=["a == b", "b == a"]
95+
)
96+
97+
98+
def benchmark_fmpq_addition(runner: pyperf.Runner):
6999
"""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):
100+
runner.timeit(
101+
name="fmpq addition",
102+
setup=[
103+
"from flint import fmpq",
104+
"a = fmpq(1, 2)",
105+
"b = fmpq(15, 7)",
106+
],
107+
stmt="a + b"
108+
)
109+
110+
111+
def benchmark_fmpq_multiplication(runner: pyperf.Runner):
77112
"""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):
113+
runner.timeit(
114+
name="fmpq multiplication",
115+
setup=[
116+
"from flint import fmpq",
117+
"a = fmpq(1, 2)",
118+
"b = fmpq(15, 7)",
119+
],
120+
stmt="a * b"
121+
)
122+
123+
124+
def benchmark_fmpq_eq(runner: pyperf.Runner):
85125
"""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):
126+
runner.timeit(
127+
name="fmpq equality",
128+
setup=[
129+
"from flint import fmpq",
130+
"a = fmpq(1, 2)",
131+
"b = fmpq(15, 7)",
132+
],
133+
stmt=["a == b", "b == a"]
134+
)
135+
136+
137+
def benchmark_acb_addition(runner: pyperf.Runner):
98138
"""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):
139+
runner.timeit(
140+
name="acb addition",
141+
setup=[
142+
"from flint import acb",
143+
"a = acb(1 + 3j)",
144+
"b = acb.pi()",
145+
],
146+
stmt="a + b"
147+
)
148+
149+
150+
def benchmark_acb_multiplication(runner: pyperf.Runner):
106151
"""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)
152+
runner.timeit(
153+
name="acb multiplication",
154+
setup=[
155+
"from flint import acb",
156+
"a = acb(1 + 3j)",
157+
"b = acb.pi()",
158+
],
159+
stmt="a * b"
160+
)
161+
162+
163+
def benchmark_acb_eq(runner: pyperf.Runner):
164+
"""Simple benchmark for containment on two acbs."""
165+
runner.timeit(
166+
name="acb contains",
167+
setup=[
168+
"from flint import acb",
169+
"a = acb(1 + 3j)",
170+
"b = acb.pi()",
171+
],
172+
stmt=["a in b", "b in a"]
173+
)
174+
175+
def main():
176+
"""Run all the benchmarks."""
177+
runner = pyperf.Runner()
178+
benchmarks = get_all_benchmarks(__name__)
179+
for benchmark in benchmarks:
180+
benchmark(runner)
124181

125182
if __name__ == "__main__":
126-
print_benchmark_results(run_all_benchmarks(__name__))
183+
main()

benchmarks/utils.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,3 @@ def get_all_benchmarks(modulename):
1111
for name, obj in inspect.getmembers(sys.modules[modulename])
1212
if inspect.isfunction(obj) and name.startswith("benchmark_")
1313
]
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)