Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-hashed string keys for faster Python dictionaries lookup?

How can I instruct python to store, internally, a pre-hashed version of my strings, so that it will use that value when I perform dict/set lookups using my string as a key?

I remember reading about it some weeks ago, but can't find it in python docs at the moment :-/

like image 662
jcayzac Avatar asked Aug 12 '11 01:08

jcayzac


People also ask

How fast is Python dictionary lookup?

When it comes to 10,000,000 items a dictionary lookup can be 585714 times faster than a list lookup. 6.6 or 585714 are just the results of a simple test run with my computer. These may change in other cases.

Are Dicts fast Python?

Python Dictionaries are fast but their memory consumption can also be high at the same time.

Do Python dictionaries use hash?

Dictionaries in Python are implemented using hash tables. It is an array whose indexes are obtained using a hash function on the keys.

What is better than dictionary in Python?

defaultdict. defaultdict handles our previous error by building on top of dictionaries. It does not give a KeyError like the regular dictionaries. However, whenever you try to access or “assign to” a key that is not there, it will create that key and give it a default value that was already specified .


1 Answers

String interning is probably what you're thinking of.

See sys.intern in Python 3

See intern in Python 2

like image 58
Glenn Maynard Avatar answered Sep 21 '22 13:09

Glenn Maynard