Deleting from a list (reprise)

Hans Nowak wurmy at earthlink.net
Wed Jan 2 05:05:46 EST 2002


Alex Martelli wrote:

> Counter-intuitively to me, the trivial 'delwhile' approach appears to
> be substantially faster than the "advanced" ideas on this benchmark
> run -- and not by trivial amounts, either: 5 or 10 times faster is NOT
> a speedup to sneer at.
> 
> As this sharply contradicts your findings about "almost always faster",
> it's no doubt worth double checking both this benchmark and yours: it
> IS quite possibly that I've goofed up somewhere (though I can't see it).

Well, I tested it with lists of 10,000 elements... lists as small as
50 elements would all give me runtimes of 0.0 seconds. :-(  Of course,
I could repeat the function N times, like you are doing, but I wasn't
that interested, and it's 5 AM here, so... :-)

Here's my benchmark code:

#---begin---

import random
import time

def create_random_list(amplitude, num_elems):
    return [int(random.random() * amplitude) for i in range(num_elems)]

def test(amplitude, num_elems):
    print "Testing with amplitude", amplitude, "and", num_elems, "elements"
    lst1 = create_random_list(amplitude, num_elems)
    lst2 = lst1[:]

    # method 1: while x in list
    start = time.time()
    while 1 in lst1:
        lst1.remove(1)
    stop = time.time()
    print "Method 1:", stop - start, "seconds"

    # method 2: walk through the list in reverse
    start = time.time()
    for i in range(len(lst2)-1,-1,-1):
        if lst2[i] == 1:
            del lst2[i]
    stop = time.time()
    print "Method 2:", stop - start, "seconds"


test(10, 10000)
test(100, 10000)
test(1000, 10000)
#---end---

I don't use benchmarks often, so maybe I've done something
wrong. (Well, aside from this method's inability to
accurately measure the results for small lists...)

--Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==\n') 
       # decode for email address ;-)
Site:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list