[Tutor] Performance list vs. deque for [-1]

Sander Sweers sander.sweers at gmail.com
Thu Aug 12 12:07:10 CEST 2010


On 12 August 2010 09:44, Alan Gauld <alan.gauld at btinternet.com> wrote:
>> I'm wondering what's the fastet datatype in python to lookup the last
>> element in an ordered collection.
>
> When in doubt timeit()

Out of curiosity I used timeit and lists are faster if we iterate over
them one by one. Now this is my first go with the timeit module so
please do correct me :-). Code used is at
http://python.pastebin.org/475014.

def getLastItem(c):
    return [x[-1] for x in c]

The list/deque consists of 5000 items (lists or deques) with a length
21 random ints. Also for deque I used maxlen=26 which makes it a bit
faster.

List took 0.000693 seconds
693.01178383827209 / 100000 (default times run by timeit)

Deque took 0.009195 seconds
693.01178383827209 / 100000 (default times run by timeit)

How this applies to the OP situation I don't know..

Greets
Saner


More information about the Tutor mailing list