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

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

Re: Performance of `re-search-backward' vs `re-search-forward'


From: Stefan Monnier
Subject: Re: Performance of `re-search-backward' vs `re-search-forward'
Date: Mon, 12 Apr 2021 16:59:32 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>>> I seem to (very vaguely) remember reading that `re-search-backward' is
>>> significantly slower than `re-search-forward'.  However, I can't find
>>> anything about it in the docstring (nor in the Elisp reference) now.  Am
>>> I even correct?
>> I haven't looked at the code recently so my memory might be off, but
>> I can't think of any reason why it should be noticeably slower, no.
> So both basically move character by character and check if the regex
> matches there (more or less)?

Yes, they're more or less loops that move char-by-char and call
`looking-at` each time.  The `looking-at` is always doing a "forward
match" even if the outer loop is search backward, which is why the
performance difference should be negligible even if there might be
a difference in the performance of the outer loop.


>> Maybe you're confusing it with `looking-back` which is much slower than
>> `looking-at`?
> Yes, that was it!  Thanks!

Right: `looking-back` is actually doing a `re-search-backward` so it has
an outer loop which calls `looking-at` each time, hence it's O(n) times
slower than `looking-at`.


        Stefan




reply via email to

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