2626
2727# Mathics expressions to benchmark
2828BENCHMARKS = {
29- 'Arithmetic' : ['1 + 2' , '5 * 3' ],
30- 'Plot' : [
31- 'Plot[0, {x, -3, 3}]' ,
32- 'Plot[x^2 + x + 1, {x, -3, 3}]' ,
33- 'Plot[Sin[Cos[x^2]], {x, -3, 3}]' ,
34- 'Plot[Sin[100 x], {x, -3, 3}]' ],
35- 'Plot3D' : [
36- 'Plot3D[0, {x, -1, 1}, {y, -1, 1}]' ,
37- 'Plot3D[x + y^2, {x, -3, 3}, {y, -2, 2}]' ,
38- 'Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -3, 3}]' ,
39- 'Plot3D[Sin[100 x + 100 y ^ 2], {x, 0, 1}, {y, 0, 1}]' ],
40- 'DensityPlot' : [
41- 'DensityPlot[x + y^2, {x, -3, 3}, {y, -2, 2}]' ],
42- 'Trig' : [
43- 'Sin[RandomReal[]]' , 'ArcTan[RandomReal[]]' ],
44- 'Random' : [
45- 'RandomInteger[{-100, 100}, 100]' ,
46- 'RandomInteger[10, {10, 10}]' ,
47- 'RandomInteger[{0,1}, {5, 5, 5}]' ,
48- 'RandomReal[1, 100]' , 'RandomReal[{-1, 1}, 100]' ,
49- 'RandomComplex[2 + I, 50]' ,
50- 'RandomComplex[{-1 - I, 1 + I}, {10, 10}]' ],
51- 'Expand' : [
52- 'Expand[(a1+a2)^200]' , 'Expand[(a1+a2+a3)^25]' ,
53- 'Expand[(a1+a2+a3+a4+a5+a6+a7)^3]' ],
54- 'Matrix' : [
55- 'RandomInteger[{0,1}, {10,10}] . RandomInteger[{0,1}, {10,10}]' ,
56- 'RandomInteger[{0,10}, {10,10}] + RandomInteger[{0,10}, {10,10}]' ],
29+ "Arithmetic" : ["1 + 2" , "5 * 3" ],
30+ "Plot" : [
31+ "Plot[0, {x, -3, 3}]" ,
32+ "Plot[x^2 + x + 1, {x, -3, 3}]" ,
33+ "Plot[Sin[Cos[x^2]], {x, -3, 3}]" ,
34+ "Plot[Sin[100 x], {x, -3, 3}]" ,
35+ ],
36+ "Plot3D" : [
37+ "Plot3D[0, {x, -1, 1}, {y, -1, 1}]" ,
38+ "Plot3D[x + y^2, {x, -3, 3}, {y, -2, 2}]" ,
39+ "Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -3, 3}]" ,
40+ "Plot3D[Sin[100 x + 100 y ^ 2], {x, 0, 1}, {y, 0, 1}]" ,
41+ ],
42+ "DensityPlot" : ["DensityPlot[x + y^2, {x, -3, 3}, {y, -2, 2}]" ],
43+ "Trig" : ["Sin[RandomReal[]]" , "ArcTan[RandomReal[]]" ],
44+ "Random" : [
45+ "RandomInteger[{-100, 100}, 100]" ,
46+ "RandomInteger[10, {10, 10}]" ,
47+ "RandomInteger[{0,1}, {5, 5, 5}]" ,
48+ "RandomReal[1, 100]" ,
49+ "RandomReal[{-1, 1}, 100]" ,
50+ "RandomComplex[2 + I, 50]" ,
51+ "RandomComplex[{-1 - I, 1 + I}, {10, 10}]" ,
52+ ],
53+ "Expand" : [
54+ "Expand[(a1+a2)^200]" ,
55+ "Expand[(a1+a2+a3)^25]" ,
56+ "Expand[(a1+a2+a3+a4+a5+a6+a7)^3]" ,
57+ ],
58+ "Matrix" : [
59+ "RandomInteger[{0,1}, {10,10}] . RandomInteger[{0,1}, {10,10}]" ,
60+ "RandomInteger[{0,10}, {10,10}] + RandomInteger[{0,10}, {10,10}]" ,
61+ ],
5762}
5863
5964DEPTH = 300
6368 ";" .join (map (str , range (1 , DEPTH ))),
6469 "/" .join (map (str , range (1 , DEPTH ))),
6570 "^" .join (map (str , range (1 , DEPTH ))),
66- "! " * DEPTH + ' expr' ,
67- "!" * DEPTH + ' expr' ,
68- ' expr' + "& " * DEPTH ,
69- "Sin[" * DEPTH + ' 0.5' + "]" * DEPTH ,
71+ "! " * DEPTH + " expr" ,
72+ "!" * DEPTH + " expr" ,
73+ " expr" + "& " * DEPTH ,
74+ "Sin[" * DEPTH + " 0.5" + "]" * DEPTH ,
7075]
7176
7277definitions = Definitions (add_builtin = True )
@@ -108,14 +113,17 @@ def timeit(func, repeats=None):
108113
109114 times .append (time .process_time ())
110115
111- times = [times [i + 1 ] - times [i ] for i in range (repeats )]
116+ times = [times [i + 1 ] - times [i ] for i in range (repeats )]
112117
113118 average_time = format_time_units (mean (times ))
114119 best_time = format_time_units (min (times ))
115120 median_time = format_time_units (median (times ))
116121
117- print (" {0:5n} loops, avg: {1}, best: {2}, median: {3} per loop" .format (
118- repeats , average_time , best_time , median_time ))
122+ print (
123+ " {0:5n} loops, avg: {1}, best: {2}, median: {3} per loop" .format (
124+ repeats , average_time , best_time , median_time
125+ )
126+ )
119127
120128
121129def truncate_line (string ):
@@ -133,16 +141,17 @@ def benchmark_parse_file(fname):
133141 try :
134142 import urllib .request
135143 except ImportError :
136- print (' install urllib for Combinatorica parsing test' )
144+ print (" install urllib for Combinatorica parsing test" )
137145 return
138146 print (" '{0}'" .format (truncate_line (fname )))
139147 with urllib .request .urlopen (fname ) as f :
140- code = f .read ().decode (' utf-8' )
148+ code = f .read ().decode (" utf-8" )
141149
142150 def do_parse ():
143151 feeder = MathicsMultiLineFeeder (code )
144152 while not feeder .empty ():
145153 parse (definitions , feeder )
154+
146155 timeit (do_parse )
147156
148157
@@ -151,7 +160,8 @@ def benchmark_parser():
151160 for expression_string in PARSING_BENCHMARKS :
152161 benchmark_parse (expression_string )
153162 benchmark_parse_file (
154- 'http://www.cs.uiowa.edu/~sriram/Combinatorica/NewCombinatorica.m' )
163+ "http://www.cs.uiowa.edu/~sriram/Combinatorica/NewCombinatorica.m"
164+ )
155165
156166
157167def benchmark_format (expression_string ):
@@ -181,30 +191,37 @@ def benchmark_all_sections():
181191
182192def main ():
183193 global evaluation , TESTS_PER_BENCHMARK
184- parser = ArgumentParser (
185- description = "Mathics benchmark suite." , add_help = False )
194+ parser = ArgumentParser (description = "Mathics benchmark suite." , add_help = False )
186195
187196 parser .add_argument (
188- '--help' , '-h' , help = 'show this help message and exit' , action = 'help' )
197+ "--help" , "-h" , help = "show this help message and exit" , action = "help"
198+ )
189199
190200 parser .add_argument (
191- ' --version' , '-v' , action = ' version' ,
192- version = '%(prog)s ' + mathics . __version__ )
201+ " --version" , "-v" , action = " version" , version = "%(prog)s " + mathics . __version__
202+ )
193203
194204 parser .add_argument (
195- ' --section' , '-s' , dest = "section" , metavar = "SECTION" ,
196- help = "only test SECTION" )
205+ " --section" , "-s" , dest = "section" , metavar = "SECTION" , help = "only test SECTION"
206+ )
197207
198- parser .add_argument (
199- '-p' , '--parser' , action = 'store_true' , help = "only test parser" )
208+ parser .add_argument ("-p" , "--parser" , action = "store_true" , help = "only test parser" )
200209
201210 parser .add_argument (
202- '--expression' , '-e' , dest = "expression" , metavar = "EXPRESSION" ,
203- help = "benchmark a valid Mathics expression" )
211+ "--expression" ,
212+ "-e" ,
213+ dest = "expression" ,
214+ metavar = "EXPRESSION" ,
215+ help = "benchmark a valid Mathics expression" ,
216+ )
204217
205218 parser .add_argument (
206- '--number' , '-n' , dest = "repeat" , metavar = "REPEAT" ,
207- help = "loop REPEAT number of times" )
219+ "--number" ,
220+ "-n" ,
221+ dest = "repeat" ,
222+ metavar = "REPEAT" ,
223+ help = "loop REPEAT number of times" ,
224+ )
208225
209226 args = parser .parse_args ()
210227
@@ -221,5 +238,6 @@ def main():
221238 benchmark_all_sections ()
222239 benchmark_parser ()
223240
224- if __name__ == '__main__' :
241+
242+ if __name__ == "__main__" :
225243 main ()
0 commit comments