Lisp:defmacro与&;可选和&;身体
我编写了一个快速而肮脏的宏来计时lisp代码。然而,我现在面临的问题是,我想在函数中包含一个可选的输出流。但是,我不知道如何在Lisp:defmacro与&;可选和&;身体,lisp,common-lisp,sbcl,Lisp,Common Lisp,Sbcl,我编写了一个快速而肮脏的宏来计时lisp代码。然而,我现在面临的问题是,我想在函数中包含一个可选的输出流。但是,我不知道如何在defmacro中同时使用&optional和&body参数。我查找了一些示例,但只找到了那些我认为我理解的defun。我不明白为什么这对我来说是失败的。任何提示: (defmacro timeit (&optional (out-stream *standard-output*) (runs 1) &body body) "Note that thi
defmacro
中同时使用&optional
和&body
参数。我查找了一些示例,但只找到了那些我认为我理解的defun
。我不明白为什么这对我来说是失败的。任何提示:
(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)))
以下是我打算如何使用代码:
(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.
我认为,在defmacro
上发现的这一点是类似的
(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))
编辑:关键字参数
上面显示的用法显然有缺陷。也许这样更好:
(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.
谢谢。该怎么做
如何检测第一件事是可选流
(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?
我会避免这样的宏参数列表
通常我更喜欢:
(with-timings ()
a b c)
还有一条小溪
(with-timings (*standard-output*)
a b c)
第一个列表给出了可选参数。列表本身不是可选的
该宏应该更容易编写
通常,可能不需要指定流:
(let ((*standard-output* some-stream))
(timeit a b c))
你可以实现你想要的,但我不会:
(defmacro timeit (&rest args)
(case (length args)
(0 ...)
(1 ...)
(otherwise (destructuring-bind (stream &rest body) ...))))
解决方案:使用非可选关键字arglist
根据雷纳的评论
使用模式:
(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
我的一般观点是,这些参数通常应该在单独的列表中提供,这是宏的第一个参数。这在带有-类型宏的中尤其常见。其他一些答案已经说明了如何做到这一点,但我认为先编写实现主要功能的功能版本,然后编写宏版本也是一种很好的宏编写技术。这并不难,尽管这里的方法确实有可能增加函数调用开销的时间
(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))
但是你知道已经有一个宏可以做到这一点吗<代码>时间。您编写了一个函数?我只看到一个宏…对不起,我是说宏。更正。而且,我知道时间就是这样做的。这更多是为了练习编写宏。另外,我想包括迭代次数和日志记录等,这是时间所没有的。这意味着你不能写<代码>(timeit 1000(+11))?是,同意。我只是在你的回答下评论了一下。我认为可选关键字参数可能更合适。我将尝试实现它。嗯。。。我明白你的意思,也同意这样写会更容易,但你的意思是说这种
&可选的,然后是&主体的习惯用法太难(不可能?)实现吗?@asb:不要先写宏。举例说明在使用这样的宏时代码的外观,找出它是否有意义,然后尝试编写宏来实现语法。首先需要获取语法,然后编写宏。你现在有一个宏,我们必须猜测它可能会被使用。。。对我来说,完全不清楚这样的arglist是否有意义。从hyperspec中添加了示例调用和类似的东西。好吧,我明白为什么这没有意义了。也许关键字参数在这里更合适。谢谢你的帮助。
(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