From: Wang Yin
Subject: What kind of type declaration can speed up LISP code?
Date:
Message-ID: <m34qxmbznu.fsf@wangyin.com>
Hi,
I wonder what kind of type declaration can speed up LISP code.
If I declare a struct coord to use double-float vectors to represent a
coordinate this certainly can speed up the code and tell the compiler
to do floating point arithmetic.
(defstruct (coord (:constructor %make-coord))
(vec '#(0.0d0 0.0d0) :type (vector double-float)))
But later if I declare a node structure as follows:
(defstruct (node (:print-function print-node))
(number 0 :type fixnum)
(degree 0 :type fixnum)
(coord nil)
(coord2 nil)
(inact nil)
)
And I define a function to operation on node structure:
(defun make-edge-connects (n1 n2 &optional (number 0))
"Create an edge connecting node n1 and n2."
(declare (type node n1 n2) (type fixnum number))
(let ((new-edge (make-edge :number number
:vertices (list n1 n2))))
(add-edge-to-node n1 new-edge)
(add-edge-to-node n2 new-edge)
new-edge))
Will the declaration of (type node n1 n2) take effect to speed up the
code? Will declaration of user defined types of function parameters
speed up the code?
Thanks.
--
Yin Wang,
EDA Lab,
Deparment of Computer Science and Technology,
Tsinghua University,
100084
Beijing China
Wang Yin wrote:
> Hi,
>
> I wonder what kind of type declaration can speed up LISP code.
>
> If I declare a struct coord to use double-float vectors to represent a
> coordinate this certainly can speed up the code and tell the compiler
> to do floating point arithmetic.
>
> (defstruct (coord (:constructor %make-coord))
> (vec '#(0.0d0 0.0d0) :type (vector double-float)))
Why not
(defstruct coord
(x 0.0d0 :type double-float)
(y 0.0d0 :type double-float))
?
Or even
(deftype coord () '(array double-float (2)))
?
Or even
(deftype coord () 'complex)
?
>
>
> But later if I declare a node structure as follows:
>
> (defstruct (node (:print-function print-node))
> (number 0 :type fixnum)
> (degree 0 :type fixnum)
> (coord nil)
> (coord2 nil)
> (inact nil)
> )
>
>
> And I define a function to operation on node structure:
>
> (defun make-edge-connects (n1 n2 &optional (number 0))
> "Create an edge connecting node n1 and n2."
>
> (declare (type node n1 n2) (type fixnum number))
> (let ((new-edge (make-edge :number number
> :vertices (list n1 n2))))
> (add-edge-to-node n1 new-edge)
> (add-edge-to-node n2 new-edge)
> new-edge))
>
>
> Will the declaration of (type node n1 n2) take effect to speed up the
> code? Will declaration of user defined types of function parameters
> speed up the code?
That depends on the implementation. CMUCL/SBCL and the commercial
implementations are pretty good at it. As per your function, it does not
seem it would help much anyway. The ADD-EDGE-TO-NODE function may do
something slow.
Cheers
--
Marco