Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to time a function call in elisp?

Tags:

elisp

Yes, I am aware of the emacs profiler feature. I'm looking for something similar to the time keyword in bash, something like:

(time (myfunc)) 

which would return or print the time taken by the myfunc call. Is there such a thing?

like image 214
killdash9 Avatar asked Jan 21 '14 00:01

killdash9


2 Answers

benchmark.el provides benchmark-run and benchmark-run-compiled functions as well as a benchmark version to run interactively. The linked example:

C-u 512 M-x benchmark (sort (number-sequence 1 512) '<)
Elapsed time: 0.260000s (0.046000s in 6 GCs)

The timer used by all those functions is the benchmark-elapse macro, which you can also use directly if desired:

ELISP> (require 'benchmark)
ELISP> (benchmark-elapse
         (sit-for 2))
2.00707889
like image 150
Jon Purdy Avatar answered Oct 13 '22 22:10

Jon Purdy


I found exactly what I was looking for at http://nullprogram.com/blog/2009/05/28/

(defmacro measure-time (&rest body) "Measure and return the running time of the code block." (declare (indent defun)) (let ((start (make-symbol "start"))) `(let ((,start (float-time))) ,@body (- (float-time) ,start))))

like image 42
killdash9 Avatar answered Oct 13 '22 22:10

killdash9