gpt4 book ai didi

lisp - 收集 `time`宏产生的时空结果?

转载 作者:太空宇宙 更新时间:2023-11-03 18:43:05 25 4
gpt4 key购买 nike

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-timeget-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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com