help-gnu-emacs
[Top][All Lists]
Advanced

[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     é -> &eacute.
>>   With an argument,  reverse    é <- &eacute.
>>   Works on the whole buffer"
>>  (interactive "P")
>>  (save-excursion
>>    (let ((association
>>           '(("É" . "&Eacute;") ("á" . "&aacute;")  ("à" . "&agrave;")
>>             ("â" . "&acirc;")  ("ä" . "&auml;")    (""" . "&atilde;")
>>             ("é" . "&eacute;") ("è" . "&egrave;")  ("ê" . "&ecirc;")
>>             ("ë" . "&euml;")   ("í" . "&iacute;")  ("ì" . "&igrave;")
>>             ("î" . "&icirc;")  ("ï" . "&iuml;")    ("ñ" . "&ntilde;")
>>             ("ó" . "&oacute;") ("ò" . "&ograve;")  ("ô" . "&ocirc;")
>>             ("ö" . "&ouml;")   ("ı" . "&otilde;")  ("ú" .
>> "&uacute;")
>>             ("ù" . "&ugrave;") ("û" . "&ucirc;")   ("ü" . "&uuml;")
>>             ("ç" . "&ccedil;")))
>>         (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


reply via email to

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