list IndexError

Steven Bethard steven.bethard at gmail.com
Thu Dec 23 16:57:22 CET 2004


Steven Bethard wrote:
> Ishwor wrote:
> 
>> i am trying to remove an item 'e' from the list l
> 
> 
> I thought it might be helpful to code some of the alternatives you've 
> been given and look at the timings to put things into perspective.

Corrected timings[1] using:

$ python -m timeit -s "import remove" "remove.remove_<name>(500, [x % 
1000 for x in xrange(10000)])"

remove_xrange: 5.46 msec per loop
remove_lc:     5.48 msec per loop
remove_try:    6.31 msec per loop
remove_ishwor: 7.03 msec per loop
remove_list:   8.38 msec per loop
remove_filter: 9.08 msec per loop
remove_remove: 9.98 msec per loop

and using:

$ python -m timeit -s "import remove" "remove.remove_<name>(50, [x % 100 
for x in xrange(10000)])"

remove_lc:      5.12 msec per loop
remove_xrange:  5.8  msec per loop
remove_list:    7.9  msec per loop
remove_filter:  8.29 msec per loop
remove_try:    30.3  msec per loop
remove_ishwor: 30.7  msec per loop
remove_remove: 55.2  msec per loop

So, when timed correctly =) list comprehensions and xrange are faster 
even when the items to be removed are only 0.1% of the data.

Steve

[1] Thanks Peter!

P.S.  For fun, I played around with the data to see if I could find a 
dataset on which one of the "slower" techniques is faster than remove_lc 
or remove_xrange.  Here's one:

$ python -m timeit -s "import remove" "remove.remove_<name>(25, [x % 50 
for x in xrange(100)])"

remove_remove: 53.3 usec per loop
remove_xrange: 57.7 usec per loop
remove_try:    58.8 usec per loop
remove_ishwor: 58.9 usec per loop
remove_lc:     65.4 usec per loop
remove_list:   93.7 usec per loop
remove_filter: 95.8 usec per loop



More information about the Python-list mailing list