list-comprehension and map question (simple)
runsun pan
runsun at gmail.com
Wed Mar 30 11:32:57 EST 2005
Thx, Robert. I did some tests:
>>> xs = range(10000)
>>> ys = range(0,20000,2)
>>> def m1(x, count=100):
... for c in range(count):
... y = map(float, x)
... return y
>>> def L1(x, count=100):
... for c in range(count):
... y = [float(z) for z in x]
... return y
>>> def m2(xs, yx, count=100):
... for c in range(count):
... y = map(lambda x,y: x+y, xs, ys)
... return y
>>> def L2(xs, yx, count=100):
... for c in range(count):
... y =[x+y for x,y in itertools.izip(xs, ys)]
... return y
>>> profile.run('m1(xs)')
105 function calls in 0.991 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
100 0.484 0.005 0.484 0.005 :0(map)
1 0.000 0.000 0.000 0.000 :0(range)
1 0.485 0.485 0.485 0.485 :0(setprofile)
1 0.022 0.022 0.506 0.506 <interactive input>:1(m1)
1 0.000 0.000 0.506 0.506 <string>:1(?)
1 0.000 0.000 0.991 0.991 profile:0(m1(xs))
0 0.000 0.000 profile:0(profiler)
>>> profile.run('L1(xs)')
5 function calls in 0.724 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.724 0.724 0.724 0.724 <interactive input>:1(L1)
1 0.000 0.000 0.724 0.724 <string>:1(?)
1 0.000 0.000 0.724 0.724 profile:0(L1(xs))
0 0.000 0.000 profile:0(profiler)
>>> profile.run('m2(xs, ys)')
1000105 function calls in 6.171 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
100 3.336 0.033 6.149 0.061 :0(map)
1 0.000 0.000 0.000 0.000 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.022 0.022 6.171 6.171 <interactive input>:1(m2)
1000000 2.813 0.000 2.813 0.000 <interactive
input>:3(<lambda>)
1 0.000 0.000 6.171 6.171 <string>:1(?)
1 0.000 0.000 6.171 6.171 profile:0(m2(xs, ys))
0 0.000 0.000 profile:0(profiler)
>>> profile.run('L2(xs, ys)')
5 function calls in 0.418 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.418 0.418 0.418 0.418 <interactive input>:1(L2)
1 0.000 0.000 0.418 0.418 <string>:1(?)
1 0.000 0.000 0.418 0.418 profile:0(L2(xs, ys))
0 0.000 0.000 profile:0(profiler)
First of all, map(float, some_list_of_numbers) didn't seems to be
faster than [float(x) for x in some_list_of_numbers]. Maybe I did the
test in a wrong way ?
Secondly, [x+y for x,y in itertools.izip(xs, ys)] did go much faster
than map(lambda x,y: x+y, xs, ys). The latter is not only the slowest
one, but with an amazingly slow speed of 15 times slowdown.
More information about the Python-list
mailing list