11

I have some code using (looking-back … (line-beginning-position)). Doc string of looking-back states that it's better to avoid this function because of slowness. I'm curious will following approach be faster?

(save-excursion
  (goto-char (line-beginning-position))
  (looking-at regexp stuff))
3
  • It will be faster. But this isn't an equivalent code.
    – abo-abo
    May 27, 2015 at 17:10
  • @abo-abo, yep, it is not. But in my case I believe I can introduce looking-at version easily. Thank you. May 27, 2015 at 17:14
  • 3
    The biggest performance issue of looking-back is when it has no limit (or a limit that is far away). If you consider (with-temp-buffer (insert (make-string 10000 ?x)) (looking-back "y")) You'll see it is very fast. Now change the regexp to ".*y" instead : it's unbearably slow even though it's clear to the human reader that this can never match.
    – YoungFrog
    May 27, 2015 at 17:40

1 Answer 1

10

Definitely. You will especially gain if you just want to test char-before or search backward for a literal string. And if you must use looking-back then try to use a LIMIT argument, if possible.

See Emacs bug #17284 for an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.