Initializing the number of slots in a dictionary

John Machin sjmachin at lexicon.net
Sun Aug 6 18:33:30 EDT 2006


Jon Smirl wrote:
> Is there some way to tell a dictionary object that I am going to load 1M
> objects into it and have it pre-allocate enought slots to hold all of the
> entries?

Not according to the manual.

Not according to the source [as at 2.4.3]. In any case, if there were a
back-door undocumented arg for the dict constructor, somebody would
have read the source and spread the news.

> Thus avoiding many thousand memory allocations.

What gives you the impression that "many thousand memory allocations"
are involved? {My impression is that the dict would be resized about 11
times on its trip from size 8 to size 2M, and each resize would involve
one allocation.]

Do you have an application with a performance problem? If so, what
makes you think inserting 1M items into a Python dict is contributing
to the problem?

Have you read the thread "Large Dictionaries" which started on/about
2006-05-15?

In general, what is the background to your question?

Cheers,
John




More information about the Python-list mailing list