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__ ))
0 commit comments