On 25 September 2012 23:10, Tim Chase <[email protected]> wrote:
> On 09/25/12 16:17, Oscar Benjamin wrote:
> > I don't know whether it would be better or worse but it might be
> > worth seeing what happens if you replace the FileContext objects
> > with tuples.
>
> If tuples provide a savings but you find them opaque, you might also
> consider named-tuples for clarity.
>
Do they have the same memory usage?
Since tuples don't have a per-instance __dict__, I'd expect them to be a
lot lighter. I'm not sure if I'm interpreting the results below properly
but they seem to suggest that a namedtuple can have a memory consumption
several times larger than an ordinary tuple.
>>> import sys
>>> import collections
>>> A = collections.namedtuple('A', ['x', 'y'])
>>> sys.getsizeof(a)
72
>>> sys.getsizeof(A(1, 2))
72
>>> sys.getsizeof((1, 2))
72
>>> sys.getsizeof(A(1, 2).__dict__)
280
>>> A(1, 2).__dict__
OrderedDict([('x', 1), ('y', 2)])
>>> sys.getsizeof((1, 2).__dict__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute '__dict__'
>>> A(1, 2).__dict__ is A(3, 4).__dict__
False
Oscar
--
http://mail.python.org/mailman/listinfo/python-list