Thank you for sharing Grr1337.
In my experience, you wouldn't typically construct a function which includes conditionals to test for variations in the data type of the supplied argument: since you are developing the program, you know ahead of time the data that will eventually be supplied to the function, and can therefore construct the function accordingly.
For example, I think it would be very rare to develop a program in which a function would process a data set in the form of an association list of dotted pairs, and subsequently process a list of lists in the same manner. The developer would likely decide ahead of time which data type is better suited to storing the required data and would develop the appropriate function accordingly.
"Simplier" list construction, and then reconstruct that list, using a subfunction.but I've tried constructing before a list like the expected (in the main program), without any need of such subfunction.
Sort list before grouping them is recommended. No sort, the time's complexity is O(n2/2), sort and group is O(nLOGn).
But with this benchmark you proved me wrong!compile all functions and run the benchmark again, i guess results will be different
Since the test sample has only 26 groups (A-Z), the MEMBER function might be a little faster.i agree
When data is more cluttered, it is better to use sorting algorithm first.
(vlisp-compile 'lsa "C:\\Temp\\tmp1.lsp")
(setq test1 (apply 'append (mapcar 'entget (SS2EntList (ssget)))))
(load "C:\\Temp\\tmp1.lsp")
(benchmark '((groupbykey-Fools test1) (NestAssoc-Grrr1337_1 test1)))
(load "C:\\Temp\\tmp1.fas")
(benchmark '((groupbykey-Fools test1) (NestAssoc-Grrr1337_1 test1)))
; 1 form loaded from #<file "C:/Temp/tmp1.lsp">Lee's MEMBER is faster than both but i didn't include it in the benchmark because it is recursive and won't work on much longer lists
Benchmarking .....Elapsed milliseconds / relative speed for 4 iteration(s):
(GROUPBYKEY-FOOLS TEST1).........1092 / 1.16 <fastest>
(NESTASSOC-GRRR1337_1 TEST1).....1263 / 1.00 <slowest>
; 1 form loaded from #<file "C:/Temp/tmp1.fas">
Benchmarking ......Elapsed milliseconds / relative speed for 8 iteration(s):
(NESTASSOC-GRRR1337_1 TEST1).....1107 / 1.13 <fastest>
(GROUPBYKEY-FOOLS TEST1).........1248 / 1.00 <slowest>
(defun ALE_GroupByKey (lst / TKey OKey keys tmpL OutLst)
(foreach itm lst
(setq OKey (car itm) tmpL (cdr itm))
(if (vl-position (setq TKey (strcat "###" OKey)) keys)
(set (read TKey) (vl-list* OKey tmpL (cdr (eval (read TKey)))))
(progn
(set (read TKey) (list OKey tmpL))
(setq keys (cons TKey keys))
)
)
)
(foreach itm keys
(setq OutLst (cons (eval (read itm)) OutLst))
(set (read itm) nil)
)
OutLst
)
(setq test1 (F-RanLst 300000))
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 1 iteration(s):
(GROUPBYKEY-FOOLS TEST1).....2828 / 1.25 <fastest>
(ALE_GROUPBYKEY TEST1).......3546 / 1 <slowest>
(defun ALE_GroupByKey2 (lst / TKey OKey RKey keys tmpL OutLst)
(setq TKey (strcat "#" (caar lst)) keys (list TKey))
(set (read TKey) (list (caar lst) (cdar lst)))
(foreach itm (cdr lst)
(setq OKey (car itm) tmpL (cdr itm) TKey (strcat "#" OKey) RKey (read TKey))
(if (vl-position TKey keys)
(set RKey (vl-list* OKey tmpL (cdr (eval RKey))))
(progn
(set RKey (list OKey tmpL))
(setq keys (cons TKey keys))
)
)
)
(foreach itm keys
(setq RKey (read itm) OutLst (cons (eval RKey) OutLst))
(set RKey nil)
)
OutLst
)
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 1 iteration(s):
(ALE_GROUPBYKEY2 TEST1)......2547 / 1.05 <fastest>
(GROUPBYKEY-FOOLS TEST1).....2672 / 1 <slowest>
(defun ALE_GroupByKey3 (l / k s r p)
(foreach i l
(if (vl-position (setq k (car i)) s)
(setq p (assoc k r) r (subst (vl-list* (car i) (cdr i) (cdr p)) p r))
(setq s (cons k s) r (cons (list k (cdr i)) r))
)
)
)
(defun ALE_GroupByKey4 (l / k s r p x)
(foreach i l
(if (setq x (vl-position (setq k (car i)) s))
(setq p (nth x r) r (subst (vl-list* (car i) (cdr i) (cdr p)) p r))
(setq s (cons k s) r (cons (list k (cdr i)) r))
)
)
)
(defun ALE_GroupByKeyUD (l / k s r p x)
(foreach i l
(if (setq x (vl-position (setq k (car i)) s))
(setq p (ALE_List_Nth x r) r (subst (vl-list* (car i) (cdr i) (cdr p)) p r))
(setq s (cons k s) r (cons (list k (cdr i)) r))
)
)
)