Is it possible to give a python dict an initial capacity (and is it usefull)

Posted by Peter Smit on Stack Overflow See other posts from Stack Overflow or by Peter Smit
Published on 2010-06-11T06:00:16Z Indexed on 2010/06/11 6:02 UTC
Read the original article Hit count: 247

Filed under:
|
|

I am filling a python dict with around 10,000,000 items. My understanding of dict (or hashtables) is that when too much elements get in them, the need to resize, an operation that cost quite some time.

Is there a way to say to a python dict that you will be storing at least n items in it, so that it can allocate memory from the start? Or will this optimization not do any good to my running speed?

(And no, I have not checked that the slowness of my small script is because of this, I actually wouldn't now how to do that. This is however something I would do in Java, set the initial capacity of the HashSet right)

© Stack Overflow or respective owner

Related posts about python

Related posts about dict