map-like function on dict values?
Carel Fellinger
cfelling at iae.nl
Thu Feb 28 18:03:38 EST 2002
Skip Montanaro <skip at pobox.com> wrote:
...
> 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
yep, see my other posting:)
> 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:
Ah a new contestent, double fun, better algoritme better timing (linux
too), but the real iter still wins, although by a very slight margin:)
> iter 6.69
> iteritems 28.36
> mapvalue 8.12
...
def iteriteritems(f, d):
for k, v in d.iteritems():
d[k] = f(v)
...
> timeit(iter, f, d)
timeit(iteriteritems, f, d)
timeit(iteritems, f, d)
> timeit(mapvalue, f, d)
iter 10.95
iteriteritems 10.43
iteritems 52.15
mapvalue 12.88
--
groetjes, carel
More information about the Python-list
mailing list