【问题标题】:Emacs -- Timing execution of function calls in Emacs lispEmacs -- 在 Emacs lisp 中定时执行函数调用
【发布时间】:2014-05-13 03:24:50
【问题描述】:

我正在寻求帮助,以便在使用 while 循环时测量函数调用的时间——即,当函数抛出完成时时钟应该停止。我在邮件列表中找到了以下计时器:http://lists.gnu.org/archive/html/help-gnu-emacs/2008-06/msg00087.html

(defmacro measure-time (&rest body)
  "Measure the time it takes to evaluate BODY."
  `(let ((time (current-time)))
     ,@body
     (message "%.06f" (float-time (time-since time)))))

它是这样使用的:

(measure-time
  (dotimes (i 100000)
    (1+ 1)))

非常感谢您提供如何使用带有while 循环的计时器宏的示例。我正在寻找从while 循环开始之前到done 被抛出时结束的总时间。

(defun test ()
  (measure-time)
  (catch 'done
    (while t
      [*** do a bunch of stuff]
      (when (condition-satisfied-p)
        [*** STOP THE CLOCK AND PRINT TOTAL DURATION ***]
        (throw 'done nil) ))))

【问题讨论】:

  • 请注意,如果body 包含符号time,您将得到意想不到的结果。您可以使用gensym 避免此类变量捕获。

标签: emacs elisp


【解决方案1】:

完全按照您引用的示例。您实际上将(measure-time ... ) 包裹在您正在计时的事物周围。在您的情况下,整个 catch 表达式。

你没试过吗?

【讨论】:

  • 啊,是的,现在可以了。我没有意识到它是一个包装器——我曾尝试将它放在开头和结尾,但没有将 catch 表达式夹在中间。非常感谢——非常感谢! :)
【解决方案2】:

顺便说一句,这个宏在 Emacs 中被称为benchmark-elapse,但它不是自动加载的,所以你需要在使用它之前(require 'benchmark)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多