Guido's regrets: filter and map
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sun Nov 24 18:12:08 EST 2002
maney at pobox.com fed this fish to the penguins on Sunday 24 November
2002 07:40 am:
>
> ### revised test with filtering
> from time import time
>
> n = 1000000
> N = xrange(n)
>
> t0 = time()
> S=[]
> a = S.append
> for x in N:
> if x % 2 == 1:
> a(str(x))
> print 'for loop took %.2f to make a list of %d items' % ((time()-t0),
> len(S))
>
> del S
> t0 = time()
> S = [str(x) for x in N if x % 2 == 1]
> print 'list comprehension took %.2f to make a list of %d items' %
> ((time()-t0), len(S))
>
> del S
> t0 = time()
> S = map(str,filter(lambda x: x % 2 == 1, N))
> print 'map took %.2f to make a list of %d items' % ((time()-t0),
> len(S))
> ###
>
<snip>
> Okay, that's enough for now.
Well, to add some more embers... Two runs under Mandrake 8.2, on a
733MHz P-III with RDRAM (Dell XPS B733r), Python 2.2, using the program
listed above.
[wulfraed at beastie wulfraed]$ python t.py
for loop took 7.61 to make a list of 500000 items
list comprehension took 7.30 to make a list of 500000 items
map took 6.84 to make a list of 500000 items
[wulfraed at beastie wulfraed]$ python t.py
for loop took 7.61 to make a list of 500000 items
list comprehension took 7.31 to make a list of 500000 items
map took 6.85 to make a list of 500000 items
--
> ============================================================== <
> wlfraed at ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed at dm.net | Bestiaria Support Staff <
> ============================================================== <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <
More information about the Python-list
mailing list