Creating a List of Empty Lists

Robin Becker robin at jessikat.fsnet.co.uk
Tue Dec 9 06:20:14 EST 2003


Duncan Booth's prompted me to repeat all the nonsense about empty lists
of a few years ago. I am amazed at the differences between pythons.
Lambda is now faster than list!!! I would really like to know why
list([]) is so much slower than list(()). Clearly comprehensions are now
fast, but still slower than the corresponding map with a lambda. 


My results all obtained on the same win2k sp4 machine.
C:\tmp>\python20\python ttt.py
list ()              = 2.09
list []              = 1.19
comprehension        = 1.97
copy                 = 4.69
cCopy.copy           = 2.09
lambda z: z[:]       = 1.56
lambda z: list(z)    = 2.66
lambda z: []         = 1.42

C:\tmp>\python21\python ttt.py
list ()              = 2.33
list []              = 1.23
comprehension        = 1.78
copy                 = 4.34
cCopy.copy           = 2.22
lambda z: z[:]       = 1.55
lambda z: list(z)    = 2.33
lambda z: []         = 1.41

C:\tmp>\python22\python ttt.py
list ()              = 3.22
list []              = 1.33
comprehension        = 1.59
copy                 = 4.05
cCopy.copy           = 2.13
lambda z: z[:]       = 1.69
lambda z: list(z)    = 2.55
lambda z: []         = 1.64

C:\tmp>\python23\python ttt.py
list ()              = 1.73
list []              = 3.22
comprehension        = 1.00
copy                 = 3.94
cCopy.copy           = 1.59
lambda z: z[:]       = 1.14
lambda z: list(z)    = 4.77
lambda z: []         = 0.95

############### ttt.py
import time
s = 'list ()'
t0=time.time()
for y in xrange(1000):
        x = map(list,1000*[()])
t1 = time.time()
print "%-20s = %.2f" % (s,(t1-t0))
assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s

s = 'list []'
t0=time.time()
for y in xrange(1000):
        x = map(list,1000*[[]])
t1 = time.time()
print "%-20s = %.2f" % (s,(t1-t0))
assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s

s = "comprehension"
t0=time.time()
for y in xrange(1000):
         x = [[] for i in xrange(1000)]
t1 = time.time()
print "%-20s = %.2f" % (s,(t1-t0))
assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s

from copy import copy
s = 'copy'
t0=time.time()
for y in xrange(1000):
        x = map(copy,1000*[[]])
t1 = time.time()
print "%-20s = %.2f" % (s,(t1-t0))
assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s

try:
        from cCopy import copy as ccopy
        s = 'cCopy.copy'
        t0=time.time()
        for y in xrange(1000):
                x = map(ccopy,1000*[[]])
        t1 = time.time()
        print "%-20s = %.2f" % (s,(t1-t0))
        assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s
except ImportError:
        pass

s = 'lambda z: z[:]'
t0=time.time()
for y in xrange(1000):
        x = map(lambda z: z[:],1000*[[]])
t1 = time.time()
print "%-20s = %.2f" % (s,(t1-t0))
assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s

s = 'lambda z: list(z)'
t0=time.time()
for y in xrange(1000):
        x = map(lambda z: list(z),1000*[[]])
t1 = time.time()
print "%-20s = %.2f" % (s,(t1-t0))
assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s

s = 'lambda z: []'
t0=time.time()
for y in xrange(1000):
        x = map(lambda z: [],xrange(1000))
t1 = time.time()
print "%-20s = %.2f" % (s,(t1-t0))
assert x[0]==[] and x[0] is not x[-1], "%s failed identity" % s 
-- 
Robin Becker




More information about the Python-list mailing list