[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] 2D vector class



On 21 Aug 2006 at 23:48, Brian Fisher wrote:

> On 8/15/06, Marius Gedminas <mgedmin@xxxxxxxx> wrote:
> > > [slots for performance]
> >
> > __slots__ is for optimizing memory usage, not speed.
> >
> optimizing memory usage is not mutually exclusive to optimizing speed
> 
> One thing that It took me a long time to fully realize (years really)
> as a c++ programmer, is the incredibly large impact of memory
> allocations on performance - I mean like 3x or 4x times difference
> kind of performance.
> 
> From what I read about slots, it seemed like they could make it
> possible for python to do much fewer allocations for an object in that
> it wouldn't have to allocate properties dynamically, cause when the
> object was allocated, it could allocate space for the properties at
> the same time.
> 
Slots replace memory allocated in a dictionary. But if any attribute 
goes into an instance dict then the benefit is probably lost. More 
important, slots simplify the attribute lookup process. Slots use 
automatically generated descriptors, one for each slot. Descriptors 
override normal attributes. Every attribute lookup starts with a 
descriptor search. The instance dict is searched only if one is not 
found. So having a descriptor reduces the search time. And a slot 
descriptor does not do a dict lookup. So slots should be faster, 
though I have not confirmed it.

Lenard Lindstrom
<len-l@xxxxxxxxx>