Emacs - 在Emacs lisp中执行函数调用的时间

law*_*ist 8 emacs elisp

我正在寻找一些帮助来测量使用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)))))
Run Code Online (Sandbox Code Playgroud)

它的使用方式如下:

(measure-time
  (dotimes (i 100000)
    (1+ 1)))
Run Code Online (Sandbox Code Playgroud)

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) ))))
Run Code Online (Sandbox Code Playgroud)

phi*_*ils 6

完全按照您引用的示例进行。您实际上是在包裹(measure-time ... )要计时的东西。整个catch您的情况,表达式都是如此。

你没有尝试吗?


Ste*_*fan 6

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