#-------- Define Preprocesser Macros ---------- $define LOOPS 200 # number of time procedure is executed $define NUM 50 # number of words in list $define LENGTH 5 # number of characters in each word $define ARGS [seq](StringTools:-Random(LENGTH, 'alpha'), cnt=1..NUM) $define MEAS(i) \ kernelopts('gcfreq'=10^7): \ StringTools:-Randomize(1): \ Usage:-TimeMemoryPrint(SortNames[i], argseq = ARGS, loops = LOOPS ): \ restart; #-------- Print Notes and Table Header ----------- printf("Testfile: %s\n\n", TESTFILE): printf("Compare performance of sorting names.\n"): printf("Each test sorts the same list of %d names of %d characters %d times.\n\n", NUM, LENGTH, LOOPS): Usage:-TimeMemoryHeader(); restart; #------- Assign and Measure each Procedure ------- SortNames[1] := proc(names :: list) sort( names , (a,b) -> convert(a,string) <= convert(b,string) ); end proc: MEAS(1); SortNames[2] := proc(names :: list) sort( names , (a,b) -> sprintf("%a",a) <= sprintf("%a",b) ); end proc: MEAS(2); SortNames[3] := proc(names :: list) local n; map(attributes, sort([seq](setattribute(sprintf("%a",n),n) , n = names) , 'string')); end proc: MEAS(3); # This solves the issue with collisions and, as a bonus, # has no side-effects. A winner. SortNames[4] := proc(names :: list) local n; map(attributes, sort([seq](setattribute([sprintf]("%a",n),n) , n = names) , 'lexorder'[1])); end proc: MEAS(4); SortNames[5] := proc(names :: list) local n; map(attributes, sort([seq](setattribute(convert(n,'`local`'),n) , n = names))); end proc: MEAS(5); done