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))
Malabarba
  • 22,878
  • 6
  • 78
  • 163
Geradlus_RU
  • 625
  • 7
  • 17
  • 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
  • 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 '15 at 17:40

1 Answers1

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.

Dmitry
  • 3,508
  • 15
  • 20
Drew
  • 75,699
  • 9
  • 109
  • 225