From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Lee Newsgroups: gmane.emacs.help Subject: efficiency question on text manipulation using string vs buffer Date: Mon, 23 Mar 2009 18:41:08 -0700 (PDT) Organization: http://groups.google.com Message-ID: <01cc8ac5-0b6c-4f7f-8018-73e644b8bf17@p6g2000pre.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1237869364 15928 80.91.229.12 (24 Mar 2009 04:36:04 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 24 Mar 2009 04:36:04 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Mar 24 05:37:22 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LlyO8-0007RC-LN for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Mar 2009 05:37:20 +0100 Original-Received: from localhost ([127.0.0.1]:33870 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LlyMl-000109-G2 for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Mar 2009 00:35:55 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!newshub.sdsu.edu!postnews.google.com!p6g2000pre.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 87 Original-NNTP-Posting-Host: 24.6.175.142 Original-X-Trace: posting.google.com 1237858868 29718 127.0.0.1 (24 Mar 2009 01:41:08 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 24 Mar 2009 01:41:08 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p6g2000pre.googlegroups.com; posting-host=24.6.175.142; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:167922 comp.emacs:98055 X-Mailman-Approved-At: Tue, 24 Mar 2009 00:35:32 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:63214 Archived-At: 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\" \"=CE=B1\"] [\"beta\" \"=CE=B2\"])) 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 '(["&" "&"] ["<" "<"] [">" ">"])) (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 '(["&" "&"] ["<" "<"] [">" ">"])) (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 =E2=88=91 http://xahlee.org/ =E2=98=84