List Performance

Peter Otten __peter__ at web.de
Mon Jun 30 15:51:24 CEST 2008


Larry Bates wrote:

> Peter Otten wrote:
>> Ampedesign wrote:
>> 
>>> If I happen to have a list that contains over 50,000 items, will the
>>> size of the list severely impact the performance of appending to the
>>> list?
>> 
>> No.
>> 
>> $ python -m timeit -n20000 -s"items = []" "items.append(42)"
>> 20000 loops, best of 3: 0.554 usec per loop
>> $ python -m timeit -n20000 -s"items = [42]*10**6" "items.append(42)"
>> 20000 loops, best of 3: 0.529 usec per loop
>> 
>> http://wiki.python.org/moin/TimeComplexity
>> 
>> Peter
> 
> Peter,
> 
> So its actually faster to append to a long list than an empty one?  That
> certainly would not have been intuitively obvious now would it?

You shouldn't blindly trust the numbers. 

Here's what happens if I repeat the measurements a few times:

$ python -m timeit -n20000 -s"items = []" "items.append(42)"
20000 loops, best of 3: 0.531 usec per loop
$ python -m timeit -n20000 -s"items = []" "items.append(42)"
20000 loops, best of 3: 0.511 usec per loop
$ python -m timeit -n20000 -s"items = []" "items.append(42)"
20000 loops, best of 3: 0.512 usec per loop
$ python -m timeit -n20000 -s"items = []" "items.append(42)"
20000 loops, best of 3: 0.51 usec per loop
$ python -m timeit -n20000 -s"items = []" "items.append(42)"
20000 loops, best of 3: 0.514 usec per loop
$ python -m timeit -n20000 -s"items = [42]*10**6" "items.append(42)"
20000 loops, best of 3: 0.506 usec per loop
$ python -m timeit -n20000 -s"items = [42]*10**6" "items.append(42)"
20000 loops, best of 3: 0.512 usec per loop
$ python -m timeit -n20000 -s"items = [42]*10**6" "items.append(42)"
20000 loops, best of 3: 0.543 usec per loop
$ python -m timeit -n20000 -s"items = [42]*10**6" "items.append(42)"
20000 loops, best of 3: 0.522 usec per loop
$ python -m timeit -n20000 -s"items = [42]*10**6" "items.append(42)"
20000 loops, best of 3: 0.51 usec per loop

The difference is within the error margin. All you can say is that both
operations take roughly the same time.

In general, if no error margin (e. g. 0.5+-0.1) is given that is always a
warning sign, be it opinion polls or timeit output.

Peter



More information about the Python-list mailing list