Fast Data Comparison (dict v. list. v string)

Robert Brewer fumanchu at amor.org
Wed Apr 14 17:22:50 EDT 2004


Scott Brady Drummonds wrote:
> I'm a relative novice at Python and am working on some 
> optimizations in my
> first Python project.  At its core, this tool I'm writing 
> needs to perform
> many comparisons to fixed-size lists of fixed-length strings.
> 
> Currently, my implementation uses dictionaries to store each 
> string.  I
> maintain a separate "key-mapping" dictionary that maps keys from one
> dictionary to the other.  Then, all I have to do to compare the two
> dictionaries is as follows:
>   for metaKey in keyMap.keys():
>     if dict1[metaKey] != dict2[keyMap[metaKey]]:
>       #  Do some processing

Try writing that as:

for key, val in keyMap.itervalues():
    if dict1[key] != dict2[val]:
        #  Do some processing

...and see how much faster it is on your dataset. 33-50% faster would
not be uncommon on a large set.


FuManChu




More information about the Python-list mailing list