From: Hallvard Tr�tteberg
Subject: structure vs. classes
Date: 
Message-ID: <2846k2$o8@fenrix.si.sintef.no>
Hi Lispers,

I've been profiling a constraint propagation systen which run under 
MCL and Allegro. The system uses real intervals and generates a lot of
them during the running of a program. The program has real time 
running constraints that makes it somewhat important to make the
creation of objects fast. Using classes the implementation of the
real interval looks like this:

(defclass real-interval ()
  ((upper :initarg :upper :accessor upper)
   (lower :initarg :lower :accessor lower)))

(dotimes (a 40000) (make-instance 'real-interval :upper nil :lower
nil))

takes around 40 secs. for MCL (Quadra) and 20 secs. for Allegro (Sun
10).
These figures tell me that during a run about one third or half of the
time is taken by make-instance. Which makes me try the following 
implementation instead, using structures:

(defstruct real-interval2
  (upper 0)
  (lower 0))

(dotimes (a 40000) (make-real-interval2 :upper nil :lower nil))

takes around 1.5 secs. for MCL and 2.5 secs. for Allegro. Not
surprisingly structures are a lot faster, but I didn't think it would
be 20 times faster. Of course, if I think of the difference between 
classes and structures I find lots of reasons that classes gives some
overhead during creation, e.g. more complex inheritance, initargs, 
use of generic make-instance and initialize-instance, makes
change-class'able 
objects.

Anyway, for an application such as our real intervals there is really
no use for all defclass gives us, more specifically, we don't need
multiple
inheritance, fancy initialization, generic reader & writer methods as
opposed to reader functions and setf macros. As long as generic
functions
can specialize one structure classes I have what I need. So why did I
use
defclass in the first place? Because I didn't have enough experience to
know that the difference would be that big.

OK, what would be nice (besides faster make-instance :-)) is some
comments about usage of classes vs. structures, what functionality 
makes you use one instead of the other, do you first use classes and
then change to structures if you need the speed or do you first use
structures and then classes when you need the functionality? Or have
you never needed to bother about it?

Hallvard "somewhat wiser now" Traetteberg