asb
asb

Reputation: 4432

Lisp: defmacro with &optional and &body

I wrote a quick and dirty macro to time lisp code. However, the problem I am facing now is that I wanted to include an optional output-stream in the function. However, I can not figure out how to use both the &optional and &body parameters in the defmacro. I looked for examples but found only those for defun which I think I understand. I am not able to figure out why this is failing for me. Any hints:

(defmacro timeit (&optional (out-stream *standard-output*) (runs 1) &body body)
  "Note that this function may barf if you are depending on a single evaluation
  and choose runs to be greater than one. But I guess that will be the
  caller's mistake instead."
  (let ((start-time (gensym))
        (stop-time (gensym))
        (temp (gensym))
        (retval (gensym)))
    `(let ((,start-time (get-internal-run-time))
           (,retval (let ((,temp))
                      (dotimes (i ,runs ,temp)
                        (setf ,temp ,@body))))
           (,stop-time (get-internal-run-time)))
       (format ,out-stream
               "~CTime spent in expression over ~:d iterations: ~f seconds.~C"
               #\linefeed ,runs
               (/ (- ,stop-time ,start-time)
                  internal-time-units-per-second)
               #\linefeed)
       ,retval)))

This is how I intend to use the code:

(timeit (+ 1 1)) ; Vanilla call
(timeit *standard-output* (+ 1 1)) ; Log the output to stdout
(timeit *standard-output* 1000 (+ 1 1)) ; Time over a 1000 iterations.

I think this, found from the hyperspec, on defmacro is a similar idea.

(defmacro mac2 (&optional (a 2 b) (c 3 d) &rest x) `'(,a ,b ,c ,d ,x)) =>  MAC2 
(mac2 6) =>  (6 T 3 NIL NIL) 
(mac2 6 3 8) =>  (6 T 3 T (8)) 

EDIT: Keyword arguments

The usage shown above is clearly flawed. Perhaps, this is better:

(timeit (+ 1 1)) ; Vanilla call
(timeit :out-stream *standard-output* (+ 1 1)) ; Log the output to stdout
(timeit :out-stream *standard-output* :runs 1000 (+ 1 1)) ; Time over a 1000 iterations.

Thanks.

Upvotes: 5

Views: 2644

Answers (3)

Joshua Taylor
Joshua Taylor

Reputation: 85823

I've of the general opinion that these kind of arguments should generally be provided in a separate list that is the first argument to the macro. This is especially common in the with- type macros. Some other answers have shown how you can do that, but I think it's also a good macro-writing technique to write a functional version first that implements the main functionality, and to then write a macro version. This one isn't too hard, although the approach here does have the potential to add some time increase for function call overhead.

(defun %timeit (function &optional (runs 1) (stream *standard-output*))
  (let ((start (get-internal-run-time))
        ret
        stop)
    (prog1 (dotimes (i runs ret)
             (declare (ignorable i))
             (setf ret (funcall function)))
      (setf stop (get-internal-run-time))
      (format stream "~&Time spent in ~a iterations: ~f seconds."
              runs
              (/ (- stop start) internal-time-units-per-second)))))

(defmacro timeit ((&optional (runs 1) (stream *standard-output*)) &body body)
  `(%timeit #'(lambda () ,@body) ,runs ,stream))

CL-USER> (timeit (10000000) (1+ most-positive-fixnum))
Time spent in 10000000 iterations: 0.148 seconds.
4611686018427387904

Upvotes: 0

asb
asb

Reputation: 4432

Solution: With a non-optional keyword arglist

(defmacro timeit ((&key
                    (to-stream *standard-output*)
                    (with-runs 1))
                  &body body)
  "Note that this function may barf if you are depending on a single evaluation
  and choose with-runs to be greater than one. But I guess that will be the
  caller's mistake instead."
  (let ((start-time (gensym))
        (stop-time (gensym))
        (temp (gensym))
        (retval (gensym))
        (elapsed-time (gensym)))
    `(let* ((,start-time (get-internal-run-time))
            (,retval (let ((,temp))
                       (dotimes (i ,with-runs ,temp)
                         (setf ,temp ,@body))))
            (,stop-time (get-internal-run-time))
            (,elapsed-time (/ (- ,stop-time ,start-time)
                              internal-time-units-per-second)))
       (format ,to-stream
               (concatenate 'string
                            "~CAverage (total) time spent in expression"
                            " over ~:d iterations: ~f (~f) seconds.~C")
               #\linefeed
               ,with-runs
               ,elapsed-time
               (/ ,elapsed-time ,with-runs)
               #\linefeed)
       ,retval)))

Based on Rainer's comments.

Usage pattern:

(timeit nil (+ 1 1)) ; Vanilla case
(timeit (:to-stream *standard-output*) (+ 1 1)) ; Log to stdout
(timeit (:with-runs 1000) (+ 1 1)) ; Evaluate 1000 times
(timeit (:with-runs 1000 :to-stream *standard-output*) (+ 1 1)) ; Evaluate 1000 times and log to stdout

Upvotes: 1

Rainer Joswig
Rainer Joswig

Reputation: 139251

How should that work?

How should it be detected that the first thing is the optional stream?

(timeit a)      ; is a the optional stream or an expression to time?
(timeit a b)    ; is a the optional stream or an expression to time?
(timeit a b c)  ; is a the optional stream or an expression to time?

I would avoid such macro arglists.

Usually I would prefer:

(with-timings ()
  a b c)

and with a stream

(with-timings (*standard-output*)
  a b c)

The first list gives the optional parameters. The list itself is not optional.

That macro should be easier to write.

Generally it may not be necessary to specify a stream:

(let ((*standard-output* some-stream))
  (timeit a b c))

You can implement what you want, but I would not do it:

(defmacro timeit (&rest args)
   (case (length args)
     (0 ...)
     (1 ...)
     (otherwise (destructuring-bind (stream &rest body) ...))))

Upvotes: 3

Related Questions