Just wondering
Peter Otten
__peter__ at web.de
Fri May 15 08:24:07 EDT 2009
Gediminas Kregzde wrote:
> Now I'm developing cross platform program and use huge amounts of
> data. Program is needed to run as fast as it coud. I've read all tips
> about geting performance, but got 1 bug: map function is slower than
> for loop for about 5 times, when using huge amounts of data.
> It is needed to perform some operations, not to return data.
>
> I'm adding sample code:
> from time import time
>
> def doit(i):
> pass
>
> def main():
> a = [0] * 10000000
> t = time()
> map(doit, a)
> print "map time: " + str(time() - t)
>
> def main2():
> t = time()
> a = [0] * 10000000
> for i in a:
> pass
> print "loop time: " + str(time() - t)
>
> main() # takes approximately 5x times longer than main2()
> main2()
>
> I'm wondering were is catch?
>
> I'm using python 2.6 on windows xp sp2 machine
Two factors:
(1) building another throwaway list and
(2) function call overhead for calling doit()
You can avoid (1) by using filter() instead of map() and verify (2) by
changing the loop to
for i in a:
doit(i)
Peter
More information about the Python-list
mailing list