[Python-Dev] speeding up list append calls

Neal Norwitz nnorwitz at gmail.com
Wed Sep 14 07:50:17 CEST 2005


Tim made me do it! 

 http://groups.google.com/group/comp.lang.python/msg/9075a3bc59c334c9

For whatever reason, I was just curious how his code could be sped up.
 I kept seeing this append method being called and I thought, "there's
an opcode for that."  What happens if you replace var.append() with
the LIST_APPEND opcode.

# standard opcodes
$ ./python ./Lib/timeit.py -n 1000 -s 'import foo' 'foo.foo1(10000)'
1000 loops, best of 3: 3.66 msec per loop
# hacked version
$ ./python ./Lib/timeit.py -n 1000 -s 'import foo' 'foo.foo2(10000)'
1000 loops, best of 3: 1.74 msec per loop

The patch and foo.py are attached.

This code doesn't really work in general.  It assumes that any append
function call is a list method, which is obviously invalid.  But if a
variable is known to be a list (ie, local and assigned as list
(BUILD_LIST) or a list comprehension), could we do something like this
as a peephole optimization?  I'm not familiar enough with some dynamic
tricks to know if it there are conditions that could break this.

Probably useless, but it was interesting to me at the time.

n
-------------- next part --------------
A non-text attachment was scrubbed...
Name: LIST_APPEND.diff
Type: text/x-patch
Size: 1353 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20050913/a32201e1/LIST_APPEND.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.py
Type: text/x-python
Size: 209 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20050913/a32201e1/foo.py


More information about the Python-Dev mailing list