[Python-Dev] timsort for jython
Skip Montanaro
skip@pobox.com
Mon, 12 Aug 2002 07:29:01 -0500
Patrick> Your flip comment has got me thinking about the "one best
Patrick> idiom" for list appending. So I'll ask the question. Is there a
Patrick> reason to want to get rid of list.append()?
Certainly not for performance. append is substantially faster that +=, at
least in part because of the list creation, especially if you cache the
method lookup.
Skip
import time
def timefunc(s, args, *sargs, **kwds):
t = time.time()
apply(s, args+sargs, kwds)
return time.time()-t
def appendit(l, o, n):
append = l.append
for i in xrange(n):
append(o)
def extendit(l, o, n):
extend = l.extend
for i in xrange(n):
extend([o])
def augassignit(l, o, n):
for i in xrange(n):
l += [o]
print "append small int:",
x = 0.0
for i in 1,2,3:
x += timefunc(appendit, ([], 1, 100000))
print "%.3f" % (x/3)
print "aug assign small int:",
x = 0.0
for i in 1,2,3:
x += timefunc(augassignit, ([], 1, 100000))
print "%.3f" % (x/3)
print "extend small int:",
x = 0.0
for i in 1,2,3:
x += timefunc(extendit, ([], 1, 100000))
print "%.3f" % (x/3)