list.clear() missing?!?

Serge Orlov Serge.Orlov at gmail.com
Wed Apr 12 02:36:02 EDT 2006


Martin v. Löwis wrote:
> Felipe Almeida Lessa wrote:
> > I love benchmarks, so as I was testing the options, I saw something very
> > strange:
> >
> > $ python2.4 -mtimeit 'x = range(100000); '
> > 100 loops, best of 3: 6.7 msec per loop
> > $ python2.4 -mtimeit 'x = range(100000); del x[:]'
> > 100 loops, best of 3: 6.35 msec per loop
> > $ python2.4 -mtimeit 'x = range(100000); x[:] = []'
> > 100 loops, best of 3: 6.36 msec per loop
> > $ python2.4 -mtimeit 'x = range(100000); del x'
> > 100 loops, best of 3: 6.46 msec per loop
> >
> > Why the first benchmark is the slowest? I don't get it... could someone
> > test this, too?
>
> In the first benchmark, you need space for two lists: the old one and
> the new one; the other benchmarks you need only a single block of
> memory (*).

I don't follow you here :)

> Concluding from here gets difficult - you would have to study
> the malloc implementation to find out whether it works better in one
> case over the other.

That's not the case here. The following program prints the same
addresses whether you comment or uncomment del
---------------------------------
ids = range(10)
for i in xrange(10):
    x = range(100000)
    #del x[:]
    ids[i] = id(x)

print ids
--------------------------------


> Could also be an issue of processor cache: one
> may fit into the cache, but the other may not.

That's not the reason, since the same time difference happens with
smaller arrays. I think the difference is how items are deallocated in
these two cases.

Calling del invokes list_ass_slice that deallocates from 0 to end
whereas ordinary removal of a list was changed to backward iteration
(the reason is in the comment:
		/* Do it backwards, for Christian Tismer.
		   There's a simple test case where somehow this reduces
		   thrashing when a *very* large list is created and
		   immediately deleted. */

Usually iterating from low addresses to higher addresses is better for
CPU. On my CPU (Pentium M, 1.7Ghz) it's 20% faster:

Here is my results:

C:\py>python -mtimeit "x = range(10000); del x[:]"
1000 loops, best of 3: 213 usec per loop

C:\py>python -mtimeit "x = range(10000); del x"
1000 loops, best of 3: 257 usec per loop

C:\py>python -mtimeit "x = range(10000); "
1000 loops, best of 3: 258 usec per loop

C:\py>python -mtimeit "x = range(1000); del x[:]"
10000 loops, best of 3: 21.4 usec per loop

C:\py>python -mtimeit "x = range(1000); del x"
10000 loops, best of 3: 25.2 usec per loop

C:\py>python -mtimeit "x = range(1000); "
10000 loops, best of 3: 25.6 usec per loop

I don't have a development environment on my computer so I can't test
my thoughts. I could be wrong about the reason.




More information about the Python-list mailing list