Emacs 24 Keyboard Macros Running Slow
Asked Answered
B

4

6

I've noticed keyboard macros running very slowly in emacs 24

for example - I just tried running a macro to append a '0' to the end of each line in a 105615 line text file

C-x ( C-e TAB 0 C-n C-a C-x )

then

C-u 105615 C-x e

takes about 5 mins to complete

I never noticed them running so slow before, wondered if it had anything to do with upgrading from 23 to 24?

Is there anything I can do to improve performance?

Best answered 4/10, 2012 at 10:28 Comment(2)
Out of curiosity, did you measure this kind of thing in Emacs 23? Macros go through the interactive machinery, so they're not efficient with huge repetition counts.Inoculation
Tangentially, note that if you want to repeat a macro until the end of the file, you can supply C-u 0 instead of checking and specifying the actual line count.Lorou
E
4

Yes, you are right, it's damn slow.

For this particular task you can use replace-regexp.

M-x buffer-disable-undo
M-x replace-regexp $ <ENTER> C-q TAB 0 <ENTER>
Encounter answered 4/10, 2012 at 10:52 Comment(1)
I would say so. I use macros to execute them dozes or hundreds of time. For hundreds thousands of time you should look for another solution.Encounter
L
10

This answer certainly won't always be applicable, but if the major mode happens to be irrelevant for the macro in question, then a good way to improve the execution speed of a keyboard macro is to switch the buffer to fundamental-mode first. In some circumstances that can provide dramatic speed increases, as you eliminate the overhead of the major and minor modes.

If changing major modes isn't possible, you may still get a significant benefit from disabling some of the minor modes for the duration.

However, as per the accepted answer & comments, if a search-and-replace will suffice, that will always be far quicker than any macro.

Lorou answered 5/10, 2012 at 7:10 Comment(0)
C
5

In your keyboard macro I see two potential sources of slowness:

  • TAB: depending on the major mode, this may perform a lot of work, e.g. to figure out the indentation to use.
  • C-n: since Emacs-23, this tries to move to the next display line, so it can require a lot more work than before (it has to take into account the display rendering, with details such as variable-width fonts, images, ...). Also it is not reliable for your use, since on a line longer than the display, it C-n will move to the next display line but stay on the same logical line, such that the subsequent c-a will just move back to the beginnning of the same line (because C-a works on logical lines, not display lines). Of course, all this depends on line-move-visual and visual-line-mode.

For the TAB, I don't have a good recommendation because I don't know what it's intended to do (depends on the major mode), but I'd replace C-n C-a with C-f which will be much faster and more reliable.

Cronus answered 4/10, 2012 at 12:58 Comment(1)
good point i can see C-f would be more succint in this instance, but its still damn slow! I assume oleg's C-q TAB (as opposed to just TAB) bypasses any context specific behaviour for the TAB keyBest
E
4

Yes, you are right, it's damn slow.

For this particular task you can use replace-regexp.

M-x buffer-disable-undo
M-x replace-regexp $ <ENTER> C-q TAB 0 <ENTER>
Encounter answered 4/10, 2012 at 10:52 Comment(1)
I would say so. I use macros to execute them dozes or hundreds of time. For hundreds thousands of time you should look for another solution.Encounter
I
0

My reading of info suggests that (forward-line) may be a better replacement for C-n C-a because this ignores any visual line breaks (so may be more efficient), and moves to the start of the line (saving the C-a).

Iguanodon answered 4/10, 2012 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.