Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs -- Timing execution of function calls in Emacs lisp

Tags:

emacs

elisp

I'm looking for some assistance, please, to measure the time of a function call when using a while loop -- i.e., the clock should stop when the function throws done. I found the following timer on the mailing list: 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)))))

It's used like this:

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

An example of how to use the timer macro with a while loop would be greatly appreciated. I'm looking for the total time beginning from before the while loop commenced to the end when done is thrown.

(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) ))))
like image 629
lawlist Avatar asked May 13 '14 03:05

lawlist


People also ask

How do you call a Lisp function?

Calling a function is also known as invocation. The most common way of invoking a function is by evaluating a list. For example, evaluating the list (concat "a" "b") calls the function concat with arguments "a" and "b" . See Evaluation, for a description of evaluation.

Is Emacs functional Lisp?

Emacs Lisp is not a purely functional programming language since side effects are common. Instead, Emacs Lisp is considered an early functional flavored language. The following features contribute to the functional flavor: its notation is functional (including a lambda calculus-like notation – see LambdaExpression)

Is Emacs Lisp the same as Lisp?

Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

Is Emacs Lisp compiled or interpreted?

Emacs Lisp has a compiler that translates functions written in Lisp into a special representation called byte-code that can be executed more efficiently. The compiler replaces Lisp function definitions with byte-code. When a byte-code function is called, its definition is evaluated by the byte-code interpreter.


2 Answers

BTW, this macro is called benchmark-elapse in Emacs, but it is not autoloaded, so you need to (require 'benchmark) before using it.

like image 63
Stefan Avatar answered Sep 28 '22 01:09

Stefan


Exactly as per the example you've quoted. You literally wrap (measure-time ... ) around the thing you're timing. The entirety of the catch expression, in your case.

Did you not try that?

like image 42
phils Avatar answered Sep 28 '22 02:09

phils