[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: elisp optimization question
From: |
Rupert Swarbrick |
Subject: |
Re: elisp optimization question |
Date: |
Fri, 09 May 2008 10:59:01 +0100 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) |
Kevin Rodgers <kevin.d.rodgers@gmail.com> writes:
> harven wrote:
>> hi,
>> you can save some typing by using an alist. Here is what i use to
>> convert
>> accented-letters into html and back.
>>
>> (defun accent-html (prefix)
>> "Accented letter translation é -> é.
>> With an argument, reverse é <- é.
>> Works on the whole buffer"
>> (interactive "P")
>> (save-excursion
>> (let ((association
>> '(("É" . "É") ("á" . "á") ("à" . "à")
>> ("â" . "â") ("ä" . "ä") (""" . "ã")
>> ("é" . "é") ("è" . "è") ("ê" . "ê")
>> ("ë" . "ë") ("í" . "í") ("ì" . "ì")
>> ("î" . "î") ("ï" . "ï") ("ñ" . "ñ")
>> ("ó" . "ó") ("ò" . "ò") ("ô" . "ô")
>> ("ö" . "ö") ("ı" . "õ") ("ú" .
>> "ú")
>> ("ù" . "ù") ("û" . "û") ("ü" . "ü")
>> ("ç" . "ç")))
>> (case-fold-search nil))
>> (dolist (paire association)
>> (when prefix
>> (setq paire (cons (cdr paire) (car paire))))
>> (goto-char (point-min))
>> (while (search-forward (car paire) nil t)
>> (replace-match (cdr paire) nil t))))))
>
> Even faster than an alist is a hash table.
>
Huh? In this code, he's iterating over the alist (which is pretty fast
- there's only a small, fixed number of items). For each element of
this alist, he's doing a search/replace. Each of those is expensive.
The data structure he uses for association is thus completely
irrelevant. Not sure that this is the best approach, but your
criticism definitely doesn't hold.
I wonder whether one could use that alist to "build" a regexp which
you could use with regexp-replace: you could use the \, syntax to add
lisp code to the stuff run.
Rupert