unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
To: emacs-devel@gnu.org
Subject: Re: remove-duplicates performances
Date: Fri, 20 May 2011 19:31:41 +0200	[thread overview]
Message-ID: <87k4dlcm82.fsf@gmail.com> (raw)
In-Reply-To: 871uztqpb7.fsf@fencepost.gnu.org

David Kastrup <dak@gnu.org> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>> David Kastrup <dak@gnu.org> writes:
>>
>>> 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) '<)
>>
>> This is nice and very instructive (at least for me) thanks.
>> It is not as performant as the version with hash-table,
>
> Well, the sorting function is a mess due to not being compiled and
> fearing dynamic binding.  If you byte-compile something like
>
> (defun uniquify (list predicate)
>   (let* ((p list) lst (sentinel (list nil)))
>     (while p
>       (push p lst)
>       (setq p (cdr p)))
>     (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) sentinel))
>       (setq lst (cdr lst)))
>     (delq sentinel list)))
>
> the behavior is likely better.
Yes, that's not really a problem, the results are very acceptable
compared to remove-duplicates (68s!).

>> but very usable: 0.3 <=> 0.13 with same test on list with 20000
>> elements.  However, isn't it a problem when we want to remove
>> duplicate in a list type alist e.g ((a . 1) (b . 2) (a . 1) (c . 3) (b
>> . 2)...)
>
> Why?  You need a predicate < both for sorting and for telling
> inequality.  As long as you define a suitable predicate for that
> purpose, what should go wrong?  Any elements for which
> (or (predicate a b) (predicate b a)) is nil will be considered
> duplicate.
Yes, i understand that, what i mean is you have to write a predicate
each time, which could be inconvenient, instead of using :test 'equal.

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




  reply	other threads:[~2011-05-20 17:31 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
2011-05-20 16:00   ` Thierry Volpiatto
2011-05-20 17:01     ` David Kastrup
2011-05-20 17:31       ` Thierry Volpiatto [this message]
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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k4dlcm82.fsf@gmail.com \
    --to=thierry.volpiatto@gmail.com \
    --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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).