dictionary - (surprisingly) python dict "has_key" faster than "in" -


from popular information , searching on net+stackoverflow, seems "in" faster "has_key" key lookups in python dictionary. however, recent experience has been quite opposite , have no clue why so? consider code of following form:

for f in f:     if 'a' in f:         alist.append(f)         #if f in fdict.keys():         if fdict.has_key(f):             idx_alist.append(fdict[f])     elif 'b' in f:         blist.append(f)         #if f in fdict.keys():         if fdict.has_key(f):             idx_blist.append(fdict[f]) 

in above, switching "has_key" makes code 50000x times faster on small files. quite baffling -- know what's going on?

it's f in fdict, not f in fdict.keys(). using keys builds list of keys , goes through 1 one, whereas using f in fdict uses efficient hash-based lookup.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -