Next: , Previous: , Up: Byte Compilation   [Contents][Index]


21.1 Performance of Byte-Compiled Code

A byte-compiled function is not as efficient as a primitive function written in C, but runs much faster than the version written in Lisp. Here is an example:

(defun silly-loop (n)
  "Return time before and after N iterations of a loop."
  (let ((t1 (current-time-string)))
    (while (> (setq n (- n 1))
              0))
    (list t1 (current-time-string))))
⇒ silly-loop
(silly-loop 40000000)
⇒ ("Mon May  2 14:02:28 2005"
    "Mon May  2 14:02:43 2005")  ; 15 seconds
(byte-compile 'silly-loop)
⇒ #<compiled-function
(n)
"...(23)"
[current-time-string t1 n 0]
2
"Return time before and after N iterations of a loop.">
(silly-loop 40000000)
⇒ ("Mon May  2 14:03:24 2005"
    "Mon May  2 14:03:29 2005")  ; 5 seconds

In this example, the interpreted code required 15 seconds to run, whereas the byte-compiled code required 5 seconds. These results are representative, but actual results will vary greatly.

Now of course, machines get faster, so efficiency differences is not always this drastic.

Just compare to the old example:

(silly-loop 5000000)
⇒ ("Mon Sep 14 15:51:49 1998"
    "Mon Sep 14 15:52:07 1998")  ; 18 seconds
and evaluation 7 years later:
(silly-loop 5000000)
⇒ ("Fri May  6 12:49:16 2005"
    "Fri May  6 12:49:17 2005")  ; 1 second

In this particular case, the byte-code version of silly-loop would not be faster (within the resolution of whole seconds).