很多人都认为 SBCL 是“最快”的 Common Lisp 实现。然而,这种情况,在OpenJDK 11出来后被反转了。ABCL是 JVM 上的 Common Lisp 实现。可能由于投入不足,这个实现基本上是没有多少编译优化的,生成的代码又臭又长又慢。即便如此,凭借JVM的优化,这些又臭又长的代码也能优化成最快的 Common Lisp 代码。下面的例子可能会让你大吃一惊!
$ ~/sbcl/bin/sbcl This is SBCL 2.0.4, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty. It is mostly in the public domain; some portions are provided under BSD-style licenses. See the CREDITS and COPYING files in the distribution for more information. * (compile-file"liutos.lisp") ; compiling file "/home/xps13/tmp/liutos.lisp" (written 17 MAY 2020 10:46:46 PM): ; compiling (DEFUN GENERATE-PRIME-NUMBERS ...) ; compiling (DEFVAR *PRIME-NUMBERS* ...) ; compiling (DEFUN FACTORING ...) ; compiling (DEFUN COUNT-COPRIME-NUMBERS ...) ; compiling (DEFUN COUNT-COPRIME-NUMBERS-BY-FORMULA ...) ; compiling (DEFUN COUNT-TOTAL-REDUCED-PROPER-FRACTIONS ...)
; wrote /home/xps13/tmp/liutos.fasl ; compilation finished in 0:00:00.018 #P"/home/xps13/tmp/liutos.fasl" NIL NIL * (load"liutos") T * (time (count-total-reduced-proper-fractions1000000)) Evaluation took: 131.693 seconds of real time 131.682014 seconds of total run time (131.602740 user, 0.079274 system) [ Run times consist of 0.011 seconds GC time, and 131.672 seconds non-GC time. ] 99.99% CPU 357,127,568,135 processor cycles 319,617,360 bytes consed 303963552391
$ ~/ccl/lx86cl64 Clozure Common Lisp Version 1.12 (v1.12-2-g5d13fc7d) LinuxX8664
For more information about CCL, please see http://ccl.clozure.com.
CCL is free software. It is distributed under the terms of the Apache Licence, Version 2.0. ? (compile-file"liutos.lisp") #P"/home/xps13/tmp/liutos.lx64fsl" NIL NIL ? (load"liutos") #P"/home/xps13/tmp/liutos.lx64fsl" ? (time (count-total-reduced-proper-fractions1000000)) (COUNT-TOTAL-REDUCED-PROPER-FRACTIONS1000000) took 136,853,744 microseconds (136.853740 seconds) to run. 10,086 microseconds ( 0.010086 seconds, 0.01%) of which was spent in GC. During that period, and with 4 available CPU cores, 136,768,017 microseconds (136.768020 seconds) were spent in user mode 90,333 microseconds ( 0.090333 seconds) were spent in system mode 319,615,296 bytes of memory allocated. 533 minor page faults, 0 major page faults, 0 swaps. 303963552391
$ ~/abcl/abcl VM settings: Stack Size: 3.00G Max. Heap Size: 6.00G Using VM: Eclipse OpenJ9 VM
Armed Bear Common Lisp 1.6.2-dev Java 14 AdoptOpenJDK Eclipse OpenJ9 VM Low-level initialization completed in 0.757 seconds. Startup completed in 1.765 seconds.
;; ...此处省略一大堆loading...
Type ":help" for a list of available commands. CL-USER(1): (load"liutos") ; Loading /home/xps13/tmp/liutos.abcl ... ; Loaded /home/xps13/tmp/liutos.abcl (2.747 seconds) T
;; 先跑第一遍,让JIT编译器去编译和优化代码。注意这个过程JIT并没有把所有代码都编译。 CL-USER(2): (time (count-total-reduced-proper-fractions1000000)) 332.083 seconds real time 4853708 cons cells 303963552391
;; 跑第二遍,直接运行内存里已编译好的代码。 CL-USER(3): (time (count-total-reduced-proper-fractions1000000)) 87.066 seconds real time 4853708 cons cells 303963552391