Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/misc/benchmark.py
4045 views
1
from misc import cputime
2
3
from sage.all import *
4
5
def benchmark(n=-1):
6
"""
7
Run a well-chosen range of Sage commands and record the time it
8
takes for each to run.
9
10
INPUT:
11
n -- int (default: -1) the benchmark number; the default
12
of -1 runs all the benchmarks.
13
OUTPUT:
14
list -- summary of timings for each benchmark.
15
int -- if n == -1, also return the total time
16
17
EXAMPLE:
18
sage: from sage.misc.benchmark import *
19
sage: _ = benchmark()
20
Running benchmark 0
21
Benchmark 0: Factor the following polynomial over
22
the rational numbers: (x^97+19*x+1)*(x^103-19*x^97+14)*(x^100-1)
23
Time: ... seconds
24
Running benchmark 1
25
Find the Mordell-Weil group of the elliptic curve 5077A using mwrank
26
Time: ... seconds
27
Running benchmark 2
28
Some basic arithmetic with very large Integer numbers: '3^1000001 * 19^100001
29
Time: ... seconds
30
Running benchmark 3
31
Some basic arithmetic with very large Rational numbers: '(2/3)^100001 * (17/19)^100001
32
Time: ... seconds
33
Running benchmark 4
34
Rational polynomial arithmetic using Sage. Compute (x^29+17*x-5)^200.
35
Time: ... seconds
36
Running benchmark 5
37
Rational polynomial arithmetic using Sage. Compute (x^19 - 18*x + 1)^50 one hundred times.
38
Time: ... seconds
39
Running benchmark 6
40
Compute the p-division polynomials of y^2 = x^3 + 37*x - 997 for primes p < 40.
41
Time: ... seconds
42
Running benchmark 7
43
Compute the Mordell-Weil group of y^2 = x^3 + 37*x - 997.
44
Time: ... seconds
45
Running benchmark 8
46
47
"""
48
49
if isinstance(n, list):
50
t = cputime()
51
v = [benchmark(m) for m in n]
52
return v, cputime(t)
53
54
if n != -1:
55
print "Running benchmark %s"%n
56
try:
57
desc, t = eval("bench%s()"%n)
58
except NameError:
59
raise RuntimeError, "no benchmark %s"%n
60
print desc
61
print "Time: %s seconds"%t
62
return (n, t, desc)
63
64
t = cputime()
65
m = 0
66
v = []
67
while True:
68
try:
69
v.append(benchmark(m))
70
m += 1
71
except RuntimeError:
72
break
73
return v, cputime(t)
74
75
def bench0():
76
"""
77
Run a benchmark.
78
79
BENCHMARK:
80
sage: from sage.misc.benchmark import *
81
sage: print bench0()[0]
82
Benchmark 0: Factor the following polynomial over
83
the rational numbers: (x^97+19*x+1)*(x^103-19*x^97+14)*(x^100-1)
84
85
"""
86
desc = """Benchmark 0: Factor the following polynomial over
87
the rational numbers: (x^97+19*x+1)*(x^103-19*x^97+14)*(x^100-1)"""
88
x = polygen(QQ,"x")
89
f = (x**97+19*x+1)*(x**103-19*x**97+14)*(x**100-1)
90
t = cputime()
91
F = f.factor()
92
return (desc, cputime(t))
93
94
def bench1():
95
"""
96
Run a benchmark.
97
98
BENCHMARK:
99
sage: from sage.misc.benchmark import *
100
sage: print bench1()[0]
101
Find the Mordell-Weil group of the elliptic curve 5077A using mwrank
102
103
"""
104
desc = """Find the Mordell-Weil group of the elliptic curve 5077A using mwrank"""
105
E = mwrank_EllipticCurve([0, 0, 1, -7, 6])
106
t = cputime()
107
g = E.gens()
108
return (desc, cputime(t))
109
110
def bench2():
111
"""
112
Run a benchmark.
113
114
BENCHMARK:
115
sage: from sage.misc.benchmark import *
116
sage: print bench2()[0]
117
Some basic arithmetic with very large Integer numbers: '3^1000001 * 19^100001
118
119
"""
120
desc = """Some basic arithmetic with very large Integer numbers: '3^1000001 * 19^100001"""
121
t = cputime()
122
a = ZZ(3)**1000001 * ZZ(19)**100001
123
return (desc, cputime(t))
124
125
def bench3():
126
"""
127
Run a benchmark.
128
129
BENCHMARK:
130
sage: from sage.misc.benchmark import *
131
sage: print bench3()[0]
132
Some basic arithmetic with very large Rational numbers: '(2/3)^100001 * (17/19)^100001
133
134
"""
135
desc = """Some basic arithmetic with very large Rational numbers: '(2/3)^100001 * (17/19)^100001"""
136
t = cputime()
137
a = QQ('2/3')**100001 * QQ('17/19')**100001
138
return (desc, cputime(t))
139
140
def bench4():
141
"""
142
Run a benchmark.
143
144
BENCHMARK:
145
sage: from sage.misc.benchmark import *
146
sage: print bench4()[0]
147
Rational polynomial arithmetic using Sage. Compute (x^29+17*x-5)^200.
148
149
"""
150
desc = """Rational polynomial arithmetic using Sage. Compute (x^29+17*x-5)^200."""
151
x = PolynomialRing(QQ, 'x').gen()
152
t = cputime()
153
f = x**29 + 17*x-5
154
a = f**200
155
return (desc, cputime(t))
156
157
def bench5():
158
"""
159
Run a benchmark.
160
161
BENCHMARK:
162
sage: from sage.misc.benchmark import *
163
sage: print bench5()[0]
164
Rational polynomial arithmetic using Sage. Compute (x^19 - 18*x + 1)^50 one hundred times.
165
166
"""
167
desc = """Rational polynomial arithmetic using Sage. Compute (x^19 - 18*x + 1)^50 one hundred times."""
168
x = PolynomialRing(QQ, 'x').gen()
169
t = cputime()
170
f = x**19 - 18*x + 1
171
w = [f**50 for _ in range(100)]
172
return (desc, cputime(t))
173
174
def bench6():
175
"""
176
Run a benchmark.
177
178
BENCHMARK:
179
sage: from sage.misc.benchmark import *
180
sage: print bench6()[0]
181
Compute the p-division polynomials of y^2 = x^3 + 37*x - 997 for primes p < 40.
182
183
"""
184
desc = """Compute the p-division polynomials of y^2 = x^3 + 37*x - 997 for primes p < 40."""
185
E = EllipticCurve([0,0,0,37,-997])
186
t = cputime()
187
for p in [2,3,5,7,11,13,17,19,23,29,31,37]:
188
f = E.division_polynomial(p)
189
return (desc, cputime(t))
190
191
def bench7():
192
"""
193
Run a benchmark.
194
195
BENCHMARK:
196
sage: from sage.misc.benchmark import *
197
sage: print bench7()[0]
198
Compute the Mordell-Weil group of y^2 = x^3 + 37*x - 997.
199
200
"""
201
desc = """Compute the Mordell-Weil group of y^2 = x^3 + 37*x - 997."""
202
E = EllipticCurve([0,0,0,37,-997])
203
t = cputime()
204
G = E.gens()
205
return (desc, cputime(t))
206
207