Common Lisp 提供了一个 time
宏来找出执行表单需要多长时间,并将信息打印到跟踪输出:
time evaluates form in the current environment (lexical and dynamic). … time prints various timing data and other information to trace output. The nature and format of the printed information is implementation-defined. Implementations are encouraged to provide such information as elapsed real time, machine run time, and storage management statistics.
例如:
(time (length (make-array 1000000)))
Real time: 0.0140014 sec.
Run time: 0.0 sec.
Space: 4000008 Bytes
GC: 1, GC time: 0.0 sec.
有没有办法收集这些参数并将它们逐步插入某个堆栈或列表并从函数返回它?
最佳答案
有些东西是标准的:get-internal-run-time和 get-internal-real-time :
(defvar *my-timings* nil)
(let ((run (get-internal-run-time))
(real (get-internal-real-time)))
(multiple-value-prog1 (my-code)
(push (cons (- (get-internal-run-time) run)
(- (get-internal-real-time) real))
*my-timings*)))
其他的没有(空间和GC计数),你需要找到implementation-specific versions .
您也可以考虑使用 with-timing
- 它提供进度报告,包括 ETA。
顺便说一句,在您的代码中,内存分配 (make-array
) 相形见绌 length
(这是数组的槽访问)。
关于lisp - 收集 `time`宏产生的时空结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23569041/