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

标签 lisp common-lisp clisp gnu-common-lisp

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/

相关文章:

lisp - "illegal terminating character after a colon: #\"在 portacle 中,虽然代码中没有冒号

python - 如何在方案中像 python 一样追加?

web-services - 在长期运行的Common Lisp应用程序中,应使用什么策略来管理垃圾?

Lisp括号问题

lisp - 评估/应用 : too many arguments given to F

clojure - Lisp-1 和 Lisp-2 有什么区别?

通过谓词将列表过滤成两部分

xpath - LISP Xpath 按顺序获取多个节点

LISP - 将语法输入转换为字符串(语言理论)

lisp - 在格式函数中使用列表的元素