D.4 Tips for Making Compiled Code Fast
Here are ways of improving the execution speed of byte-compiled
- Profile your program, to find out where the time is being spent.
- Use iteration rather than recursion whenever possible.
Function calls are slow in Emacs Lisp even when a compiled function
is calling another compiled function.
- Using the primitive list-searching functions
assoc is even faster than explicit iteration. It
can be worth rearranging a data structure so that one of these primitive
search functions can be used.
- Certain built-in functions are handled specially in byte-compiled code,
avoiding the need for an ordinary function call. It is a good idea to
use these functions rather than alternatives. To see whether a function
is handled specially by the compiler, examine its
property. If the property is non-
nil, then the function is
For example, the following input will show you that
compiled specially (see Functions that Operate on Arrays):
(get 'aref 'byte-compile)
Note that in this case (and many others), you must first load the
bytecomp library, which defines the
- If calling a small function accounts for a substantial part of your
program’s running time, make the function inline. This eliminates
the function call overhead. Since making a function inline reduces
the flexibility of changing the program, don’t do it unless it gives
a noticeable speedup in something slow enough that users care about
the speed. See Inline Functions.