fastest data structure for retrieving objects identified by (x, y) tuple?

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Oct 4 02:58:24 EDT 2012


On Oct 4, 2012 3:02 AM, "Steven D'Aprano" <
steve+comp.lang.python at pearwood.info> wrote:
> # populate a random matrix using both dict and list
> adict = {}
> alist = [[None]*2400 for i in range(2400)]
> from random import randrange
> for i in range(1000):
>     x = randrange(2400)
>     y = randrange(2400)
>     adict[(x, y)] = "something"
>     alist[x][y] = "something"
>
> import sys
> print(sys.getsizeof(adict))
> print(sys.getsizeof(alist) + sum(sys.getsizeof(L) for L in alist))
>
>
> The actual sizes printed will depend on how sparse the matrices are, but
> for the same above (approximately half full), using Python 2.7, the
> figures I get are:
>
> adict: 24712
> alist: 23127324

I make it 0.02% full. If it was half full the dict might not have a memory
advantage.

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121004/ffecc27e/attachment.html>


More information about the Python-list mailing list