[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Slot-boundp vs slot access



>It makes sense, but I've done timing tests several times, and have never found
>any significant difference between accessors and SLOT-VALUE. Does the compiler
>optimize SLOT-VALUE in some simple cases?
>     Daniel.

The difference tends to be more noticeable for slots furthur from
the beginning of the instance. The optimization also goes away
completely if you write your own (e.g. :before or :after) methods
on an accessor generic function. The compiler never optimizes SLOT-VALUE.

The following timings were done on a Mac IIfx running MCL 2.0 final (don't
ask. It will be shipping soon). I got very similar timings on 2.0b1 (though
TIME has only 1/60 second resolution in 2.0b1).

? (defclass foo ()
    ((s1 :accessor s1 :initform 's1)
     s2 s3 s4 s5 s6 s7 s8 s9 s10
     (s11 :accessor s11 :initform 's11)))
#<STANDARD-CLASS FOO>
? (defparameter *foo* (make-instance 'foo))
*FOO*
? (without-interrupts
   (time (dotimes (i 100000) (slot-value *foo* 's1))))
(DOTIMES (I 100000) (SLOT-VALUE *FOO* 'S1)) took 1498 milliseconds (1.498 seconds) to run.
NIL
? (without-interrupts
   (time (dotimes (i 100000) (s1 *foo*))))
(DOTIMES (I 100000) (S1 *FOO*)) took 1070 milliseconds (1.070 seconds) to run.
 8 bytes of memory allocated.
NIL
? (without-interrupts
   (time (dotimes (i 100000) (slot-value *foo* 's11))))
(DOTIMES (I 100000) (SLOT-VALUE *FOO* 'S11)) took 1958 milliseconds (1.958 seconds) to run.
NIL
? (without-interrupts
   (time (dotimes (i 100000) (s11 *foo*))))
(DOTIMES (I 100000) (S11 *FOO*)) took 1065 milliseconds (1.065 seconds) to run.
 8 bytes of memory allocated.
NIL
?