From: Xah Lee <xah@xahlee.org>
To: help-gnu-emacs@gnu.org
Subject: Re: processing a large buffer contents to a hash table
Date: Fri, 9 Jan 2009 09:47:06 -0800 (PST) [thread overview]
Message-ID: <84abced6-c9c2-4c8b-93f1-24174287060b@y1g2000pra.googlegroups.com> (raw)
In-Reply-To: mailman.4415.1231519828.26697.help-gnu-emacs@gnu.org
On Jan 9, 8:56 am, Seweryn Kokot <sewko...@gmail.com> wrote:
> Hello,
>
> I'm trying to write a function in elisp which returns word frequency of
> a buffer content . It works but for a large file
> (around 250 000 words) it takes 15 seconds, while a similar function in python
> takes 4s.
>
> Here is the function which process a buffer word by word and write word
> frequency to a hash table.
>
> (defun word-frequency-process-buffer ()
> (interactive)
> (let ((buffer (current-buffer)) bounds beg end word)
> (save-excursion
> (goto-char (point-min))
> (while (re-search-forward "\\<[[:word:]]+\\>" nil t)
> ;; (while (forward-word 1)
> (setq bounds (bounds-of-thing-at-point 'word))
> (setq beg (car bounds))
> (setq end (cdr bounds))
> (setq word (downcase (buffer-substring-no-properties beg end)))
> (word-frequency-incr word)
> ))))
>
> The main function is word-frequency which operates on the current buffer
> and gives *frequencies* with word statistics.
>
> Any idea how to optimize `word-frequency-process-buffer' function?
>
you doing many unnecessary things.
you don't need save-excursion.
don't need setting the bunch of boundary.
you can use just match-string to capture the word and feed it to word-
frequency-incr.
...
Xah
∑ http://xahlee.org/
☄
next prev parent reply other threads:[~2009-01-09 17:47 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.4415.1231519828.26697.help-gnu-emacs@gnu.org>
2009-01-09 17:06 ` processing a large buffer contents to a hash table Andreas Politz
2009-01-09 17:59 ` Seweryn Kokot
2009-01-09 17:47 ` Xah Lee [this message]
2009-01-09 16:56 Seweryn Kokot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=84abced6-c9c2-4c8b-93f1-24174287060b@y1g2000pra.googlegroups.com \
--to=xah@xahlee.org \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).