Benchmarking .. done for 512 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(FOO2 ATTDATA) 512 1218 1218 1.46
(_PAIR ATTDATA) 512 1781 1781 1.00
--------------------------------------------------------------------------------
Of course, calling the function with the list already sorted is even better (..for benchmark purpose. In reality you will sort it only once, inside or outside the function)
(setq attdata (vl-sort attdata (function (lambda ( a b ) (if (= (car a) (car b)) (> (cadr a) (cadr b)) (> (car a) (car b)))))))
(length attdata) -> 100
(defun foo3 ( l / m r )
(while l
(setq m (list (car l))
l (cdr l)
)
(while (and l (= (caar m) (caar l)) (= (cadar m) (cadar l)))
(setq m (cons (car l) m)
l (cdr l)
)
)
(setq r (cons (reverse m) r))
)
)
Benchmarking .. done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(FOO3 ATTDATA) 2048 1078 1078 7.19
(_PAIR ATTDATA) 512 1938 7752 1.00
--------------------------------------------------------------------------------
And some minor improvement, using PBE's idea with local variables instead of multiple (caar m) and (cadar m)
(defun foo4 ( l / m r a b)
(while l
(setq m (list (car l))
a (caar m)
b (cadar m)
l (cdr l)
)
(while (and l (= a (caar l)) (= b (cadar l)))
(setq m (cons (car l) m)
l (cdr l)
)
)
(setq r (cons (reverse m) r))
)
)
Benchmarking ... done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(FOO4 ATTDATA) 2048 1015 1015 7.70
(FOO3 ATTDATA) 2048 1110 1110 7.05
(_PAIR ATTDATA) 512 1955 7820 1.00
--------------------------------------------------------------------------------
_$
Benchmarking .. done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(FOO4 ATTDATA) 2048 1016 1016 1.09
(FOO3 ATTDATA) 2048 1110 1110 1.00
--------------------------------------------------------------------------------