1212from wolframclient .utils .encoding import force_text
1313from wolframclient .utils .functional import first
1414from wolframclient .utils .importutils import safe_import_string_and_call
15-
15+ from wolframclient .language .array import NumericArray , PackedArray
16+ from wolframclient .utils .api import numpy
1617
1718def repeat (el , n = 1 ):
1819 return tuple (el for _ in range (n ))
@@ -21,36 +22,45 @@ def repeat(el, n=1):
2122class Command (SimpleCommand ):
2223
2324 col_size = 8
25+ title_size = 14
2426 repetitions = 10
2527 complexity = [1 , 2 , 5 , 10 , 100 , 1000 ]
2628
2729 def add_arguments (self , parser ):
2830 parser .add_argument ("--profile" , dest = "profile" , default = False , action = "store_true" )
2931
30- def complexity_handler (self , complexity ):
32+ def expression_handler (self , complexity ):
3133 return {
32- "symbols" : repeat (wl .Symbol , complexity ),
33- "strings" : repeat ("string" , complexity ),
34- "bytes" : repeat (b"bytes" , complexity ),
35- "integers" : repeat (1 , complexity ),
36- "decimals" : repeat (decimal .Decimal ("1.23" ), complexity ),
37- "floats" : repeat (1.23 , complexity ),
38- "dict" : repeat ({1 : 2 , 3 : 4 , 5 : 6 }, complexity ),
39- "list" : repeat ([1 , 2 , 3 ], complexity ),
40- "functions" : repeat (wl .Function (1 , 2 , 3 ), complexity ),
34+ 'expr' : {
35+ "symbols" : repeat (wl .Symbol , complexity ),
36+ "strings" : repeat ("string" , complexity ),
37+ "bytes" : repeat (b"bytes" , complexity ),
38+ "integers" : repeat (1 , complexity ),
39+ "decimals" : repeat (decimal .Decimal ("1.23" ), complexity ),
40+ "floats" : repeat (1.23 , complexity ),
41+ "dict" : repeat ({1 : 2 , 3 : 4 , 5 : 6 }, complexity ),
42+ "list" : repeat ([1 , 2 , 3 ], complexity ),
43+ "functions" : repeat (wl .Function (1 , 2 , 3 ), complexity ),
44+ },
45+ 'array' : {
46+ '%s_%s' % (func .__name__ , t ): func (numpy .arange (complexity * 100 ), t )
47+ for func in (PackedArray , NumericArray )
48+ for t in ('Integer64' , 'Real64' )
49+ }
4150 }
4251
52+
4353 def formatted_time (self , function , * args , ** opts ):
4454
4555 time = sum (first (timed (function )(* args , ** opts )) for i in range (self .repetitions ))
4656
4757 return "%.5f" % (time / self .repetitions )
4858
4959 def table_line (self , * iterable ):
50- self .print (* (force_text (c ).ljust (self .col_size ) for c in iterable ))
60+ self .print (* (force_text (c ).ljust (i and self .col_size or self . title_size ) for i , c in enumerate ( iterable ) ))
5161
5262 def table_divider (self , length ):
53- self .print (* ("-" * self .col_size for i in range (length )))
63+ self .print (* ("-" * ( i and self .col_size or self . title_size ) for i in range (length )))
5464
5565 def stream_generators (self , path ):
5666 yield "Memory" , lambda complexity , export_format , path = path : None
@@ -62,7 +72,7 @@ def report(self):
6272
6373 path = tempfile .gettempdir ()
6474
65- benchmarks = [(c , self .complexity_handler (c )) for c in self .complexity ]
75+ benchmarks = [(c , self .expression_handler (c )) for c in self .complexity ]
6676
6777 self .table_line ("dumping results in %s" % path )
6878 self .table_line ()
@@ -102,24 +112,26 @@ def report(self):
102112 )
103113 self .table_divider (len (self .complexity ) + 1 )
104114
105- for label , export_format , opts in (
106- ("wl" , "wl" , dict ()),
107- ("wxf" , "wxf" , dict ()),
108- ("wxf zip" , "wxf" , dict (compress = True )),
109- ):
110- self .table_line (
111- label ,
112- * (
113- self .formatted_time (
114- export ,
115- expr ,
116- stream = stream_generator (complexity , export_format ),
117- target_format = export_format ,
118- ** opts
115+ for key in ('expr' , 'array' ):
116+ for label , export_format , opts in (
117+ ("wl" , "wl" , dict ()),
118+ ("wxf" , "wxf" , dict ()),
119+ ("wxf zip" , "wxf" , dict (compress = True )),
120+ ):
121+ if key == 'expr' or (key == 'array' and not label == 'wl' ):
122+ self .table_line (
123+ key == 'expr' and label or '%s %s' % (label , key ),
124+ * (
125+ self .formatted_time (
126+ export ,
127+ expr [key ],
128+ stream = stream_generator (complexity , export_format ),
129+ target_format = export_format ,
130+ ** opts
131+ )
132+ for complexity , expr in benchmarks
133+ )
119134 )
120- for complexity , expr in benchmarks
121- )
122- )
123135
124136 self .table_line ()
125137
0 commit comments