Re: Interning own classes like strings for speed and size?

2010-12-29 Thread Hrvoje Niksic
Christian Heimes writes: > You are right as long as you don't try to rebind the variable. And then only if you assign through an instance, which doesn't make sense for a class-level cache. Assignment like Example2._cache = {} would work. -- http://mail.python.org/mailman/listinfo/python-list

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread Rami Chowdhury
On Wed, Dec 29, 2010 at 00:45, Christian Heimes wrote: > Am 28.12.2010 21:16, schrieb Hrvoje Niksic: >> Christian Heimes writes: >> >>> Also this code is going to use much more memory than an ordinary tuple >>> since every instance of InternedTuple has a __dict__ attribute. This >>> code works bu

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread Christian Heimes
Am 28.12.2010 21:16, schrieb Hrvoje Niksic: > Christian Heimes writes: > >> Also this code is going to use much more memory than an ordinary tuple >> since every instance of InternedTuple has a __dict__ attribute. This >> code works but I had to move the cache outside the class because of >> __sl

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread Hrvoje Niksic
Christian Heimes writes: > Also this code is going to use much more memory than an ordinary tuple > since every instance of InternedTuple has a __dict__ attribute. This > code works but I had to move the cache outside the class because of > __slots__. Wouldn't it work inside the class as well?

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread John Nagle
On 12/27/2010 3:05 AM, Ulrich Eckhardt wrote: Hi! I'm trying to solve a computational problem and of course speed and size is important there. Then you probably shouldn't be using CPython. Apart from picking the right algorithm, I came across an idea that could help speed up things and k

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread Christian Heimes
Am 28.12.2010 15:11, schrieb Steven D'Aprano: > # Untested > class InternedTuple(tuple): > _cache = {} > def __new__(cls, *args): > t = super().__new__(cls, *args) > return cls._cache.setdefault(args, t) > def __eq__(self, other): > return self is other > de

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread Stefan Behnel
Steven D'Aprano, 28.12.2010 15:11: On Tue, 28 Dec 2010 13:42:39 +0100, Ulrich Eckhardt wrote: Steven D'Aprano wrote: class InternedTuple(tuple): ... _cache = {} ... def __new__(cls, *args): ... t = super().__new__(cls, *args) ... return cls._cache.setdefault(t,

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread Steven D'Aprano
On Tue, 28 Dec 2010 13:42:39 +0100, Ulrich Eckhardt wrote: > Steven D'Aprano wrote: > class InternedTuple(tuple): >> ... _cache = {} >> ... def __new__(cls, *args): >> ... t = super().__new__(cls, *args) >> ... return cls._cache.setdefault(t, t) > > That looks

Re: Interning own classes like strings for speed and size?

2010-12-28 Thread Ulrich Eckhardt
Steven D'Aprano wrote: class InternedTuple(tuple): > ... _cache = {} > ... def __new__(cls, *args): > ... t = super().__new__(cls, *args) > ... return cls._cache.setdefault(t, t) That looks good. The only thing that first bothered me is that it creates an obje

Re: Interning own classes like strings for speed and size?

2010-12-27 Thread Steven D'Aprano
On Mon, 27 Dec 2010 12:05:10 +0100, Ulrich Eckhardt wrote: > What I'm now considering is to only allow a single instance of these > objects for each set of values, similar to interned strings. What I > would gain is that I could safely compare objects for identity instead > of equality. What I'm n

Re: Interning own classes like strings for speed and size?

2010-12-27 Thread Tim Delaney
On 27 December 2010 22:05, Ulrich Eckhardt wrote: > What I'm now considering is to only allow a single instance of these > objects > for each set of values, similar to interned strings. What I would gain is > that I could safely compare objects for identity instead of equality. What > I'm not ye

Re: Interning own classes like strings for speed and size?

2010-12-27 Thread Ulrich Eckhardt
Terry Reedy wrote: > What sort of numbers are the coordinates? If integers in a finite range, > your problem is a lot simpler than if float of indefinite precision. Yes, indeed, I could optimize the amount of data required to store the data itself, but that would require application-specific hand

Re: Interning own classes like strings for speed and size?

2010-12-27 Thread Terry Reedy
On 12/27/2010 6:05 AM, Ulrich Eckhardt wrote: Hi! I'm trying to solve a computational problem and of course speed and size is important there. Apart from picking the right algorithm, I came across an idea that could help speed up things and keep memory requirements down. What I have is regions d

Re: Interning own classes like strings for speed and size?

2010-12-27 Thread Daniel Fetchinson
>> I believe what you are looking for is (some variant of) the singleton >> pattern: >> >> http://en.wikipedia.org/wiki/Singleton_pattern > > Actually, no. What I want is the flyweight pattern instead: > > http://en.wikipedia.org/wiki/Flyweight_pattern Oh I see. I did not know about this pattern,

Re: Interning own classes like strings for speed and size?

2010-12-27 Thread Ulrich Eckhardt
Daniel Fetchinson wrote: > I believe what you are looking for is (some variant of) the singleton > pattern: > > http://en.wikipedia.org/wiki/Singleton_pattern Actually, no. What I want is the flyweight pattern instead: http://en.wikipedia.org/wiki/Flyweight_pattern ...but thank you for the appr

Re: Interning own classes like strings for speed and size?

2010-12-27 Thread Daniel Fetchinson
> I'm trying to solve a computational problem and of course speed and size is > important there. Apart from picking the right algorithm, I came across an > idea that could help speed up things and keep memory requirements down. What > I have is regions described by min and max coordinates. At first

Interning own classes like strings for speed and size?

2010-12-27 Thread Ulrich Eckhardt
Hi! I'm trying to solve a computational problem and of course speed and size is important there. Apart from picking the right algorithm, I came across an idea that could help speed up things and keep memory requirements down. What I have is regions described by min and max coordinates. At first