次: , 前: Byte Compilation, 上: Byte Compilation


15.1 バイトコンパイルコードの性能

バイトコンパイルした関数は、Cで書いた基本関数ほど効率よくはありませんが、 Lispで書いた版よりはよほど速く動きます。 例を示しましょう。

     (defun silly-loop (n)
       "Return time before and after N iterations of a loop."
       (let ((t1 (current-time-string)))
         (while (> (setq n (1- n))
                   0))
         (list t1 (current-time-string))))
     => silly-loop
     
     (silly-loop 100000)
     => ("Fri Mar 18 17:25:57 1994"
     
         "Fri Mar 18 17:26:28 1994")  ; 31秒
     
     (byte-compile 'silly-loop)
     
     => [コンパイルしたコードは省略]
     
     (silly-loop 100000)
     => ("Fri Mar 18 17:26:52 1994"
     
         "Fri Mar 18 17:26:58 1994")  ; 6秒

この例では、解釈実行するコードでは実行に31秒必要でしたが、 バイトコンパイルしたコードでは6秒でした。 この結果は代表的なのもですが、実際の結果は大きく変動します。