【发布时间】:2019-04-14 00:35:57
【问题描述】:
使用 SBCL 1.4.12,我正在查看来自 Stuart Shapiro 的Common Lisp: An Interactive Approach 的练习 17.9,并将 reverse 函数应用于 10,000 个元素的列表。当我使用同一个列表对该函数计时时,time 函数每次报告的字节数都不同。
这是reverse函数的代码:
(defun reverse2 (l1 l2)
"Returns a list consisting of the members of L1 in reverse order
followed by the members of L2 in original order."
(check-type l1 list)
(check-type l2 list)
(if (endp l1) l2
(reverse2 (rest l1)
(cons (first l1) l2))))
(defun reverse1 (l)
"Returns a copy of the list L1
with the order of members reversed."
(check-type l list)
(reverse2 l '()))
我在 REPL 中生成了列表:
(defvar *test-list* '())
(dotimes (x 10000)
(setf *test-list* (cons x *test-list*)))
以下是四次测试的结果:
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
520,386 processor cycles
145,696 bytes consed
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
260,640 processor cycles
178,416 bytes consed
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
279,822 processor cycles
178,416 bytes consed
CL-USER> (time (ch17:reverse1 *test-list*))
Evaluation took:
0.000 seconds of real time
0.000000 seconds of total run time (0.000000 user, 0.000000 system)
100.00% CPU
264,700 processor cycles
161,504 bytes consed
第二次和第三次测试运行(相隔几分钟)显示相同的字节数,但其他两个显示不同的数字。我预计时间会有所不同,但我没想到所占用的字节数会有所不同。我看到 HyperSpec 提到了 time 函数:
一般来说,这些时间并不能保证足够可靠 营销比较。它们的价值主要是启发式的,用于调整 目的。
但我希望这适用于时间,而不是字节数。 time 报告的字节 consed 值是否不可靠?幕后是否有对此负责的优化?我错过了什么?
【问题讨论】:
-
通过在
time之前调用(sb-ext:gc :full t),您的结果差异通常较小,从而消除了测量中的一个“噪音”来源。
标签: lisp common-lisp sbcl