【问题标题】:Why is this lisp benchmark (in sbcl) so slow?为什么这个 lisp 基准测试(在 sbcl 中)这么慢?
【发布时间】:2016-11-06 07:34:37
【问题描述】:

由于我对 C++ 和 Lisp 都感兴趣,因此我尝试重现由 Guillaume Michel 编写的 here 提供的基准测试。基准测试基本上是在大阵列上多次执行的 DAXPY BLAS 1 级操作。谢天谢地,完整的代码已发布在 github 上,两种语言的版本大约在一页。

遗憾的是,我发现我无法达到他计算 lisp 的速度。

对于 Linux,他在 C++ 和 Lisp 中得到了类似的结果:

尺寸 | C++ |普通的 Lisp

100,000,000 | 181.73 | 183.9

我的 PC 上的数字(自然)不同:

尺寸 | C++ |普通的 Lisp

100,000,000 | 195.41 | 3544.3

由于我想要进行额外的测量,我使用time 命令启动了这两个程序并得到(缩短):

$ time ./saxpy
real    0m7.381s
$ time ./saxpy_lisp
real    0m40.661s

对于这种明显的差异,我假设了不同的原因。我扫描了两个代码示例,但发现 C++ 和 Lisp 版本之间在算法或数值上没有大的差异。然后我想,buildapp 的使用造成了延迟,所以我直接在 REPL 中启动了基准测试。我最后的办法是尝试另一个版本的sbcl,所以我下载了最新的sbcl-1.3.11 并在那里进行了评估——仍然(优化的)Lisp 版本比它的 C++ 版本需要更长的时间。

我错过了什么?

【问题讨论】:

  • 你编译 Lisp 代码了吗?
  • 我从 REPL 中尝试(加载“lispfile”),从 bash 中加载 --load 并尝试使用编译后的 (?) file.fasl 文件
  • .fasl 文件是编译后的版本。
  • 确保在编译时使用(declaim (optimize (speed 3) (safety 0))) 设置最佳编译器优化设置
  • 是的,这些都在重要功能中(见github代码)。我也尝试将它设置为全局(就像你建议的那样)——没有太大区别

标签: common-lisp performance-testing sbcl


【解决方案1】:

我无法复制你的发现:

sylwester@pus:~/a/bench-saxpy:master$ sbcl --dynamic-space-size 14000
This is SBCL 1.3.1.debian, 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 "saxpy.lisp")

; compiling file "/pussycat/natty-home/westerp/apps/bench-saxpy/saxpy.lisp" (written 06 NOV 2016 12:04:30 AM):
; compiling (DEFUN SAXPY ...)
; compiling (DEFUN DAXPY ...)
; compiling (DEFMACRO TIMING ...)
; compiling (DEFUN BENCH ...)
; compiling (DEFUN MAIN ...)

; /pussycat/natty-home/westerp/apps/bench-saxpy/saxpy.fasl written
; compilation finished in 0:00:00.038
#P"/pussycat/natty-home/westerp/apps/bench-saxpy/saxpy.fasl"
NIL
NIL
* (load "saxpy.fasl")

T
* (main t)
Size, Execution Time (ms)
10, 0.0
100, 0.0
1000, 0.0
10000, 0.1
100000, 0.49999997
1000000, 3.6
10000000, 39.2
100000000, 346.30002
(NIL NIL NIL NIL NIL NIL NIL NIL)

所以在我的机器上花了 346 毫秒(相对较旧的机器)。整个测试大约需要 5 秒,但这是一系列测试。有趣的是,从 sbcl 运行比制作图像并运行它要快:

sylwester@pus:~/a/bench-saxpy:master$ make lisp
buildapp --output saxpy_lisp --entry main --load saxpy.lisp --dynamic-space-size 14000
;; loading file #P"/pussycat/natty-home/westerp/apps/bench-saxpy/saxpy.lisp"
[undoing binding stack and other enclosing state... done]
[saving current Lisp image into saxpy_lisp:
writing 4944 bytes from the read-only space at 0x20000000
writing 3168 bytes from the static space at 0x20100000
writing 60522496 bytes from the dynamic space at 0x1000000000
done]
sylwester@pus:~/a/bench-saxpy:master$ ./saxpy_lisp 
Size, Execution Time (ms)
10, 0.0
100, 0.0
1000, 0.0
10000, 0.0
100000, 0.4
1000000, 3.5
10000000, 40.2
100000000, 369.49997

在我得到的 C 版本中:

100000000, 296.693634, 295.762695, 340.574860

似乎 C 版本实际上以 3 种不同的方式计算相同并报告所花费的时间。使用 time 并不公平。

【讨论】:

  • 首先:非常感谢您花时间和复制。第二:我完全执行了与您相同的步骤,但在编译函数daxpysaxpy 时确实遇到了许多注释,例如note: forced to do GENERIC-* (cost 30)note: forced to do GENERIC-+ (cost 10)。您是否在帖子中删除了它们?并且:已经有 10000 步,我有 0.7 与你的 0.0 相比——所以即使有一些差异。我应该提到我有一个相当新的 mashine,所以我期望更快的输出......
  • @claudio 我包含了所有输出并且只使用了 git repo。我在我的 Mac 上做了同样的事情,而且速度要快得多,在第一个过程之后大约 208 毫秒,因为我没有 buildapp。您收到的消息在lispforum post 中提及
  • 好的,我下载了您的版本 (sbcl-1.3.1),并再次复制了您的步骤。有了它,没有编译器注释,现在速度也快得多了。我想,这回答了我最初的问题——速度慢的原因在于 sbcl 的版本。我会将您的答案标记为我的问题的解决方案,并将其转交给 sbcl 的开发人员。再次感谢您!
  • @claudio 我在 macOS 上测试时使用的 SBCL 版本是 1.3.11,因此它在所有平台上的回归可能不同。另外,我的两台机器都是 64 位的,而关于结果的注释不是 fixnum 表明您可能使用的是 32 位机器?
  • 不,我有一台 64 位机器。我已经在我的笔记本电脑上尝试了基准测试,32 位,sbcl-1.3.6,并且还获得了编译器注释和慢速基准测试。
猜你喜欢
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多