Saturday, February 21, 2009

if you are having trouble getting reasonable numbers for timing, use this time-it macro..

Cut the following code, paste it into your file and use it as

(time-it (your function that you want to time))

Lisp's macro facility is among the very best. Unlike defun, defmacro first constructs lisp code and then evaluates it. the backquote macro is a great way to
make templates...

(defmacro time-it (code)
  `(let ((begtime (get-internal-run-time)))
   (float (/ (- (get-internal-run-time) begtime)



  1. Sorry if this is a dumb question. The third line with a comma and then the word code... Is that supposed to be a semicolon and thus a comment indicating we should put our call to A* in the middle of this macro, or is this a new way of passing in and calling a function within a function, like #'function? I haven't gotten a chance to watch the new video yet if you've answered it there...

  2. No--it is not meant to be a semicolon; it is meant to be a comma as shown.

    backquote is like quote except that it evaluates any (sub)form that is preceded by a comma
    (while quote doesn't evaluate anything)

    CL-USER> (setq j '(rao))
    CL-USER> `(a b j)
    (A B J)
    CL-USER> `(a b ,j)
    (A B (RAO))

    if you use , and an @ sign then the result of the evaluation is spliced in

    CL-USER> `(a b ,@j)
    (A B RAO)

  3. For me this still gives the time in seconds, just like the time macro did. I tested it with one of the longer searches and they both return 27.something. For the quick ones they both return zero still. I checked the units per second for my implementation:

    CL-USER> internal-time-units-per-second

    Any idea why I don't get the right time? Maybe the division by 10000000 is happening but the numerator is still zero for some reason?


Note: Only a member of this blog may post a comment.