77from wolframclient .cli .utils import SimpleCommand
88from wolframclient .deserializers import binary_deserialize
99from wolframclient .language import wl
10+ from wolframclient .language .array import NumericArray , PackedArray
1011from wolframclient .serializers import export
1112from wolframclient .utils .debug import timed
1213from wolframclient .utils .encoding import force_text
@@ -21,23 +22,31 @@ 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 (tuple (range (complexity * 100 )), t )
47+ for func in (PackedArray , NumericArray )
48+ for t in ("Integer64" , "Real64" )
49+ },
4150 }
4251
4352 def formatted_time (self , function , * args , ** opts ):
@@ -47,10 +56,15 @@ def formatted_time(self, function, *args, **opts):
4756 return "%.5f" % (time / self .repetitions )
4857
4958 def table_line (self , * iterable ):
50- self .print (* (force_text (c ).ljust (self .col_size ) for c in iterable ))
59+ self .print (
60+ * (
61+ force_text (c ).ljust (i and self .col_size or self .title_size )
62+ for i , c in enumerate (iterable )
63+ )
64+ )
5165
5266 def table_divider (self , length ):
53- self .print (* ("-" * self .col_size for i in range (length )))
67+ self .print (* ("-" * ( i and self .col_size or self . title_size ) for i in range (length )))
5468
5569 def stream_generators (self , path ):
5670 yield "Memory" , lambda complexity , export_format , path = path : None
@@ -62,13 +76,13 @@ def report(self):
6276
6377 path = tempfile .gettempdir ()
6478
65- benchmarks = [(c , self .complexity_handler (c )) for c in self .complexity ]
79+ benchmarks = [(c , self .expression_handler (c )) for c in self .complexity ]
6680
6781 self .table_line ("dumping results in %s" % path )
6882 self .table_line ()
6983
7084 # running export to do all lazy loadings
71- export (1 )
85+ binary_deserialize ( export (1 , target_format = 'wxf' ) )
7286
7387 self .table_line ("* Binary deserialize" )
7488 self .table_line ()
@@ -102,24 +116,26 @@ def report(self):
102116 )
103117 self .table_divider (len (self .complexity ) + 1 )
104118
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
119+ for key in ("expr" , "array" ):
120+ for label , export_format , opts in (
121+ ("wl" , "wl" , dict ()),
122+ ("wxf" , "wxf" , dict ()),
123+ ("wxf zip" , "wxf" , dict (compress = True )),
124+ ):
125+ if key == "expr" or (key == "array" and not label == "wl" ):
126+ self .table_line (
127+ key == "expr" and label or "%s %s" % (label , key ),
128+ * (
129+ self .formatted_time (
130+ export ,
131+ expr [key ],
132+ stream = stream_generator (complexity , export_format ),
133+ target_format = export_format ,
134+ ** opts
135+ )
136+ for complexity , expr in benchmarks
137+ )
119138 )
120- for complexity , expr in benchmarks
121- )
122- )
123139
124140 self .table_line ()
125141
0 commit comments