Emacs: Byte Compile Elisp Files
What is Byte Compiled File
Emacs lisp files can be byte compiled.
- Any emacs lisp expression can be byte compiled.
- Any emacs lisp file can be byte compiled into a file.
Byte compiled elisp file has file name
. Normal elisp file is
Advantage of Byte Compiled Elisp File
- Byte compiled elisp files load faster.
- Byte compiled elisp functions also run faster. (by a simple test of a loop 50 million times, it is about 8 times faster.)
- Byte compiling will often tell you errors or warning in your elisp code that you normally wouldn't know.
As of today (2023-08-23), for init files and light-weight packages, byte compile don't make any noticeable speed difference.
How to Byte Compile Emacs Lisp File
There are several ways to byte compile elisp files. The simplest and most useful are:
- Compile a single file. It'll ask for a file name.
- Compile all marked files in dired.
- Batch compile all elisp files in current dir and subdirectory.
Loading Byte Compiled File
When emacs loads emacs lisp files,
normally it will load the byte compiled version if exist.
Typically by looking for the file name with the
The most fundamental loading file mechanism in emacs is the function
Your init file, if you are using
do not add the file name extension such as
This way, emacs will automatically load the byte compiled version
;; load elisp file, use byte compiled version (.elc) if exist (load "my_emacs_keybinding") ;; no file name extension here
[see Emacs Lisp: load, load-file, autoload]
Recompile When Upgrade
you should recompile your elisp files when:
- upgrade to a new emcas version
- upgrade packages
- moving byte compiled elisp directory from one machine to another
because often, emacs has some incompatible elisp changes, and big packages may fail without recompile.
2011-07-15 thanks to Adolfo Benedetti [2020-10-14 https://twitter.com/adben ] .