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

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

efficiency question on text manipulation using string vs buffer


From: Xah Lee
Subject: efficiency question on text manipulation using string vs buffer
Date: Mon, 23 Mar 2009 18:41:08 -0700 (PDT)
User-agent: G2/1.0

emacs lisp question.

it's said that for text manipulation, operation on buffer data type is
more efficient than operation on string data type.

today, i tried to test it, but the difference seems negligible ? My
tentative test seems to indicate, that after performing 120 thousand
string replacement, the string method is only 1 second slower.

Here's 2 implementation of the same command. The first act on buffer
using narrow-to-region. The second deal with string.

(defun replace-string-pairs-region1 (start end mylist)
  "Replace string pairs in region.
Example call:
 (replace-string-pairs-region START END '([\"alpha\" \"α\"] [\"beta\"
\"β\"]))
The search string and replace string are all literal and case
sensitive."
  (save-restriction
    (narrow-to-region start end)
    (mapc
      (lambda (arg)
        (goto-char (point-min))
        (while (search-forward (elt arg 0) nil t) (replace-match (elt
arg 1) t t) ))
      mylist)))

(defun replace-string-pairs-region2 (start end mylist)
  "Replace string pairs in region.
Same as `replace-string-pairs-region1' but different implementation."
  (let (mystr)
    (setq mystr (buffer-substring start end))
    (mapc
     (lambda (x) (setq mystr (replace-regexp-in-string (elt x 0) (elt
x 1) mystr t t)))
     mylist)
    (delete-region start end)
    (insert mystr)
    )
)

It appears to me, testing these commands on a text selection with
about 122k chars that needs to be replaced, the second version is only
1 second slower? (both finishes within 2 or 3 seconds, on a 2007
midrange PC)

Any comments?

Here are the 2 test functions i used:

(defun f1 (start end)
  ""
  (interactive "r")
  (let (starttime endtime)
    (setq starttime (current-time))
    (replace-string-pairs-region1 start end '(["&" "&"]
                                              ["<" "&lt;"]
                                              [">" "&gt;"]))
    (setq endtime (current-time))
    (message "%f" (- (elt endtime 1)
                    (elt starttime 1)) )))

(defun f2 (start end)
  ""
  (interactive "r")
  (let (starttime endtime)
    (setq starttime (current-time))
    (replace-string-pairs-region2 start end '(["&" "&amp;"]
                                              ["<" "&lt;"]
                                              [">" "&gt;"]))
    (setq endtime (current-time))
    (message "%f" (- (elt endtime 1)
                    (elt starttime 1)) )))

the test region is a buffer with lines like this:
<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&<>&
the file size is 122k bytes. I select the whole buffer, than call f1
or f2, and compare their timing difference.

Thanks.

  Xah
∑ http://xahlee.org/

reply via email to

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