Elisp: Benchmark: Byte Compiled Code

By Xah Lee. Date: .

Speed of Byte Compiled Code

;; -*- coding: utf-8; lexical-binding: t; -*-
;; 2023-08-07
;; comparing speed of compiled code

(require 'benchmark)

(defun xtest (n)
  "Return the time, in seconds, to run N iterations of a loop."
  (while (> (setq n (1- n)) 0))
  n
  )

(setq xi 10000000)

(benchmark-run 1 (xtest xi))
;; (0.7 0 0.0)

;; using benchmark-run-compiled does not make a difference. strange
(benchmark-run-compiled 1 (xtest xi))
;; (0.6 0 0.0)

;; actually compile the code make a difference
(byte-compile 'xtest)
(benchmark-run 1 (xtest xi))
;; (0.1 0 0.0)

;; bytecompiled code is some 7 times faster in this example

Elisp, Benchmark and Unit Testing

Emacs, Byte Compile