Lee, Thanks as usual !
I would think vl-remove would bog down on large lists. It's not big deal on today's hardware. Both seem to work fine!
-David
(defun ALE_MostOccurrences (In_Lst / OutLst TmpItm TmpLen MaxLen)
(while In_Lst
(setq
TmpItm (car In_Lst)
OutLst
(cons
(cons
(setq TmpLen
(-
(length In_Lst)
(length (setq In_Lst (vl-remove TmpItm (cdr In_Lst))))
)
)
TmpItm
)
OutLst
)
)
(if (> TmpLen MaxLen) (setq MaxLen TmpLen))
)
(list Maxlen OutLst)
)
; Orginal "Massoc" by M. Puckett
(defun ALE_MAssoc (DxfKey ImpLst / TmpLst OutLst)
(while (setq TmpLst (assoc DxfKey ImpLst))
(setq OutLst (cons (cdr TmpLst) OutLst)
ImpLst (cdr (member TmpLst ImpLst))
)
)
(reverse OutLst)
)
Comando: (setq Info (ALE_MostOccurrences '(4 3 2 1 4 3 2 4 3 4 5 5 5 5)))I would think vl-remove would bog down on large lists. It's not big deal on today's hardware. Both seem to work fine!It is actually quite strange. Methinks the vl-remove function's are much more optimal than you might think. E.g.
_$ (setq l '(4 3 2 1 4 3 2 4 3 4))
(4 3 2 1 4 3 2 4 3 4)
_$ (length (repeat 5 (setq l (append l l))))
320
_$ (QuickBench '((List-Count-Atoms1 l 4) (List-Count-Atoms2 l 4) (List-Count-Atoms3 l 4)))
Benchmarking ... done for 32768 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(LIST-COUNT-ATOMS3 L 4) 32768 1419 1419 7.83
(LIST-COUNT-ATOMS1 L 4) 8192 1810 7240 1.53
(LIST-COUNT-ATOMS2 L 4) 4096 1388 11104 1.00
--------------------------------------------------------------------------------
Even compiling the LSP only slightly improves the mapcar version. It does improve the foreach quite a lot, but still not enough to surpass the vl-remove:_$ (QuickBench '((List-Count-Atoms1 l 4) (List-Count-Atoms2 l 4) (List-Count-Atoms3 l 4)))
Benchmarking ... done for 32768 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(LIST-COUNT-ATOMS3 L 4) 32768 1372 1372 3.96
(LIST-COUNT-ATOMS2 L 4) 16384 1764 3528 1.54
(LIST-COUNT-ATOMS1 L 4) 8192 1358 5432 1.00
--------------------------------------------------------------------------------
(defun ALE_MostOccurrences_3 (L / O I P M)
(while L
(setq
I (car L)
P (- (length L) (length (setq L (vl-remove I (cdr L)))))
)
(cond ( (> P M) (setq M P O (list I)) ) ( (= P M) (setq O (cons I O)) ))
)
O
)
Comando: (ALE_MostOccurrences_3 '(4 3 2 1 4 3 2 4 3 4 5 5 5 5))Another entry for 'count atom':Not much slower - even with the recursive loop:
Benchmarking .... done for 32768 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(LIST-COUNT-ATOMS3 L 4) 32768 1390 1390 7.90
(LIST-COUNT-ATOMS1 L 4) 8192 1779 7116 1.54
(COUNTATOM 4 L) 4096 1044 8352 1.32
(LIST-COUNT-ATOMS2 L 4) 4096 1373 10984 1.00
--------------------------------------------------------------------------------
And compiled it actually seems to gain more than the mapcar doesBenchmarking .... done for 32768 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(LIST-COUNT-ATOMS3 L 4) 32768 1388 1388 4.14
(LIST-COUNT-ATOMS2 L 4) 16384 1747 3494 1.64
(LIST-COUNT-ATOMS1 L 4) 8192 1327 5308 1.08
(COUNTATOM 4 L) 8192 1435 5740 1.00
--------------------------------------------------------------------------------