list addition methods compared.
Steven Bethard
steven.bethard at gmail.com
Sun Dec 26 19:38:33 EST 2004
Ishwor wrote:
> Could u run the code in your machine and perhaps and let me know what
> the average speed is??
> The code is -
[snip code not using timeit]
Are you aware of the timeit module? It can do most of these timings for
you. Here's the code I used:
------------------------------ extend.py ------------------------------
def add(items):
lst = []
for item in items:
lst = lst + [item]
def iadd(items):
lst = []
for item in items:
lst += [item]
def extend(items):
lst = []
for item in items:
lst.extend([item])
def extend_ext(items):
lst = []
ext = lst.extend
for item in items:
ext([item])
def append(items):
lst = []
for item in items:
lst.append(item)
----------------------------------------------------------------------
and here's the commands I ran (using Python 2.4)[1]:
$ python -m timeit -s "import extend; items = range(10000)"
"extend.add(items)"
10 loops, best of 3: 588 msec per loop
$ python -m timeit -s "import extend; items = range(10000)"
"extend.iadd(items)"
100 loops, best of 3: 9.68 msec per loop
$ python -m timeit -s "import extend; items = range(10000)"
"extend.extend(items)"
100 loops, best of 3: 11.5 msec per loop
$ python -m timeit -s "import extend; items = range(10000)"
"extend.extend_ext(items)"
100 loops, best of 3: 9.09 msec per loop
$ python -m timeit -s "import extend; items = range(10000)"
"extend.append(items)"
100 loops, best of 3: 4.5 msec per loop
A few things worth noting:
(1) I didn't see the top of this thread, but I'm assuming that you've
got a conditional or something in your real loop or you could just use
lst.extend(items) without ever iterating over the items list. Your real
code may actually require extend-style functionality, but as the results
above show, if you really only have one item to add, list.append is
definitely the better way to go.
(2) Yes, using += is slightly faster than list.extend, but only because
"lst.extend" requires an extra attribute lookup. If you factor that out
(like I did with the line "ext = lst.extend") then list.extend is
slightly faster. (Not surprising, as the function list_inplace_concat
(+=) calls the function listextend (list.extend) in listobject.c)
(3) That being said, extend_ext is almost certainly premature
optimization. =)
Steve
[1] You can still use timeit without Python 2.4, but you'll have to call
timeit.py directly instead of the "python -m timeit" command I used.
More information about the Python-list
mailing list