map-like function on dict values?

Skip Montanaro skip at pobox.com
Thu Feb 28 22:00:05 CET 2002


    >> Check out the operator module.  In particular, operator.setitem will
    >> probably do what you want:

    Huaiyu> This works.  Thank you.  It speeds up by more than a factor of
    Huaiyu> four againt for loop iteration.

Don't assume for loop iteration is your only culprit... ;-) List creation
overhead can be as big a problem.  I added another test function called iter
which relies on 2.2's new "for k in dict" capability to completely avoid
list creation.  (I also changed your range() to an xrange() to avoid the
extra list allocation, and called clock() instead of time() to measure CPU
time instead of elapsed time.  This is on a Linux system) Here's what I get
on my laptop:

    iter 6.69
    iteritems 28.36
    mapvalue 8.12

Modified version appended...

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)

import operator
from time import clock

def iter(f, d):
        for k in d:
                d[k] = f(d[k])

def iteritems(f, d):
        for k, v in d.items():
                d[k] = f(v)

def mapvalue(f, d):
        keys = d.keys()
        map(operator.setitem, [d]*len(keys), keys, map(f, d.values()))
                
def timeit(test, f, d):
        _time = clock()
        test(f, d)
        print test.__name__, clock() - _time

if __name__ == '__main__':
        d = {}
        for i in xrange(1000000):
                d[i] = None
        def f(x): pass

        timeit(iter, f, d)
        timeit(iteritems, f, d)
        timeit(mapvalue, f, d)




More information about the Python-list mailing list