Python が速度改善に本気を出すと聞いたので恒例のたらい回しベンチをとるらしいから、Common Lisp(Clozure CL)でチャレンジしたら爆速な話
この記事はジョーク記事です。
Pythonが速度改善に本気出すと聞いたので恒例のたらい回しベンチをとってみたら、RubyがYJITですごく速くなっていて驚いた話 に Common Lisp がなかったのでClozure CLで勝手にエントリーしてみる。
なお、Mac Book Pro 2020 13inch というとりたてて普通のIntel Macである。
たらい回し関数 はこれでいこう。本来なら 最適化をしてもよいが いけるだろう。
(defun tak (x y z) (if (<= x y) y (tak (tak (1- x) y z) (tak (1- y) z x) (tak (1- z) x y))))
さて実行して計測してみよう。
ほら、たったの 3 マイクロ秒で完了である。やったね。C言語が1秒だからむっちゃ早い。Common Lisp は神の言語である。たらい回し関数なんか普通に最適化されてしまうのである。さあ、神の速度のCommon Lisp を使おうじゃないか。
CL-USER> (time #.(tak 14 7 0)) 14 took 3 microseconds (0.000003 seconds) to run. During that period, and with 8 available CPU cores, 6 microseconds (0.000006 seconds) were spent in user mode 5 microseconds (0.000005 seconds) were spent in system mode 14
種あかし。
賢明な読者はとっくに気がついているだろう、(time (tak 14 7 0))
ではないことに。そうなんである。実際には1.5秒かかっているのであった。
CL-USER> (time (tak 14 7 0)) (TAK 14 7 0) took 1,515,945 microseconds (1.515945 seconds) to run. During that period, and with 8 available CPU cores, 1,470,043 microseconds (1.470043 seconds) were spent in user mode 16,862 microseconds (0.016862 seconds) were spent in system mode 14
#.
によって、(tak 14 7 0)
が評価されたあとに(time <tak 14 7 0 の結果>)
が評価されているのであった。 つまり (time 14)
にかかる時間を測っていることになる。