From: David Kastrup <dak@gnu.org>
To: emacs-devel@gnu.org
Subject: Re: remove-duplicates performances
Date: Fri, 20 May 2011 16:28:45 +0200 [thread overview]
Message-ID: <878vu1qwde.fsf@fencepost.gnu.org> (raw)
In-Reply-To: 877h9lv5tl.fsf@gmail.com
Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
> i just noticed that `remove-duplicates' is very slow.
>
> Something like below seem much faster:
>
> (defun* remove-dups (seq &key (test 'eq))
> (let ((cont (make-hash-table :test test)))
> (loop for elm in seq
> unless (gethash elm cont)
> do (puthash elm elm cont)
> finally return (loop for i being the hash-values in cont collect i))))
>
> Test:
>
> (setq A (let ((seq (loop for i from 1 to 10000 collect i)))
> (append seq seq)))
> (1 2 3 4 5 6 7 8 9 10 1 2 ...)
>
> (remove-dups A)
> (1 2 3 4 5 6 7 8 9 10 11 12 ...)
> elp-results: remove-dups 1 0.013707 0.013707
>
> (remove-duplicates A)
> (1 2 3 4 5 6 7 8 9 10 11 12 ...)
> elp-results: remove-duplicates 1 66.971619 66.971619
>
> Would be nice to improve performances of `remove-duplicates'.
There is little point in the overhead of a hashtable for a one-shot
operation. Hashtables are best for _maintaining_ data, not for
processing other data structures.
I've found the following in some file of mine:
(defun uniquify (list predicate)
(let* ((p list) lst (x1 (make-symbol "x1"))
(x2 (make-symbol "x2")))
(while p
(push p lst)
(setq p (cdr p)))
;;; (princ lst)(princ "\n")
(setq lst
(sort lst `(lambda(,x1 ,x2)
(funcall ',predicate (car ,x1) (car ,x2)))))
;;; lst now contains all sorted sublists, with equal cars being
;;; sorted in order of increasing length (from end of list to start).
;;
(while (cdr lst)
(unless (funcall predicate (car (car lst)) (car (cadr lst)))
(setcar (car lst) x1))
(setq lst (cdr lst)))
(delq x1 list)))
(uniquify '(2 1 2 1 2) '<)
(uniquify '(4 7 3 26 4 2 6 24 4 5 2 3 2 4 6) '<)
Obviously, this should make use of lexical binding "nowadays" instead of
creating its own unique symbols for function parameters. And instead of
using x1 as a sentinel value some other unique thing might be used, like
a one-shot '(nil).
Basically, for a one-shot O(n^2) operation on a list, `sort' will
usually do the trick. But if you are doing that O(n^2) operation
regularly, you should probably maintain the whole data set in a
hashtable instead.
--
David Kastrup
next prev parent reply other threads:[~2011-05-20 14:28 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-20 13:51 remove-duplicates performances Thierry Volpiatto
2011-05-20 14:09 ` Stefan Monnier
2011-05-20 14:39 ` Thierry Volpiatto
2011-05-20 16:14 ` Stefan Monnier
2011-05-20 17:46 ` Thierry Volpiatto
2011-05-20 15:16 ` Ted Zlatanov
2011-05-20 14:28 ` David Kastrup [this message]
2011-05-20 16:00 ` Thierry Volpiatto
2011-05-20 17:01 ` David Kastrup
2011-05-20 17:31 ` Thierry Volpiatto
2011-05-20 17:46 ` David Kastrup
2011-05-20 18:01 ` Stefan Monnier
2011-05-20 21:57 ` Pascal J. Bourguignon
2011-05-20 17:57 ` Ted Zlatanov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=878vu1qwde.fsf@fencepost.gnu.org \
--to=dak@gnu.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.