emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Would seq-range and seq-mapcat be useful?


From: Oleh Krehel
Subject: Re: Would seq-range and seq-mapcat be useful?
Date: Fri, 30 Jan 2015 17:51:33 +0100

On Fri, Jan 30, 2015 at 5:36 PM, Nicolas Richard
<address@hidden> wrote:
> Le 30/01/2015 17:04, Oleh Krehel a écrit :
>> Mine's faster:
>
> I think it will depend on the number of keys.
>
> (defmacro util-timeit (&rest expr)
>   `(progn
>      (garbage-collect) ;; avoid gc later. maybe.
>      (let ((t-beg (float-time))
>            (res (dotimes (i 500) ;; I reduced this a bit because I have a 
> slow system.
>                   ,@expr))
>            (t-end (float-time)))
>        (/
>         (- t-end t-beg)
>         500))))
>
> (progn ;; use M-x compile-defun to eval this part
>   (defun yf/seq-group-by (fn lst)
>     (let ((hash (make-hash-table :test 'equal)))
>       (dolist (elm lst)
>         (push elm (gethash (funcall fn elm) hash)))
>       hash))
>   (defun seq-group-by (fn lst)
>     (nreverse
>      (cl-reduce
>       (lambda (acc it)
>         (let* ((key (funcall fn it))
>                (cell (assoc key acc)))
>           (if cell
>               (setcdr cell (push it (cdr cell)))
>             (push (list key it) acc))
>           acc))
>       lst
>       :initial-value nil))))
>
> (defun make-random-list (n)
>   (let ((tmp))
>     (dotimes (_ n)
>       (push (cons (random 150) t) tmp)) ;; I guess only the keys matter
>     tmp))
>
> (let ((tmp (make-random-list 10)))
>   (list
>    (util-timeit (yf/seq-group-by #'car tmp))
>    (util-timeit (seq-group-by #'car tmp))))
> (3.778600692749023e-05 3.6581039428710935e-05)
>
>
> (let ((tmp (make-random-list 100)))
>   (list
>    (util-timeit (yf/seq-group-by #'car tmp))
>    (util-timeit (seq-group-by #'car tmp))))
> (0.0002734408378601074 0.0005863599777221679)
>
>
> (let ((tmp (make-random-list 1000)))
>   (list
>    (util-timeit (yf/seq-group-by #'car tmp))
>    (util-timeit (seq-group-by #'car tmp))))
> (0.00434891939163208 0.010494112968444824)
>
> I wasn't really trying to compete though, just giving another point of
> view.

You're right, of course, I was too fast to judge:).
Exactly this is why we need such a function in the ELPA / core: it
could be complex
enough to do either the alist or the hash-table approach in an optimal
way, since
the homebrew stuff is bound to be simple and work good only in some cases.

Oleh



reply via email to

[Prev in Thread] Current Thread [Next in Thread]