10

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))
  • It will be faster. But this isn't an equivalent code. – abo-abo May 27 '15 at 17:10
  • @abo-abo, yep, it is not. But in my case I believe I can introduce looking-at version easily. Thank you. – Geradlus_RU May 27 '15 at 17:14
  • 2
    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 '15 at 17:40
9

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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