Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mit-eddie!mintaka!bloom-picayune.mit.edu!news.mit.edu!hugh From: hugh@ear.mit.edu (Hugh Secker-Walker) Newsgroups: comp.emacs Subject: Speed of replace-match vs. replace-string Message-ID: <1991Feb13.200837.8025@athena.mit.edu> Date: 13 Feb 91 20:10:35 GMT Sender: news@athena.mit.edu (News system) Distribution: usa Organization: Massachusetts Institute of Technology Lines: 37 I was trying to speed up some code we use here. I know it's dangerous to assume anything about the speed with which forms evaluate based on their "complexity", but here's my (counter-intuitive) finding using "GNU Emacs 18.55.5 of Wed Oct 10 1990 on ear (berkeley-unix)", an Ultrix 4.0 DECstation 3100. The test case boils down to two functions, byte-compiled, which get rid of occurrences of "ab". (defun kill-ab-1 () "The simple ab killer." (save-excursion (goto-char (point-min)) (replace-string "ab" ""))) (defun kill-ab-2 () "The complex ab killer." (save-excursion (goto-char (point-min)) (while (search-forward "ab" nil t) (replace-match "")))) Which runs faster on a buffer, freshly reverted from its file, which contains 684,000 characters and 7,200 occurrences of "ab"? Essentially we're comparing (replace-string "ab" "") with (while (search-forward "ab" nil t) (replace-match "")). Surprise (you must have guessed) it turns out that kill-ab-2 runs in about half the time of kill-ab-1. This factor of two is just about my threshold for wondering if the optimization is worth porting to existing code (some users are running on VAXstation IIs). Explanations? Comments? -- Hugh Secker-Walker INTERNET: hugh@ear-ache.mit.edu MIT, Research Lab of Electronics UUCP: mit-eddie!mit-athena!hugh