list IndexError

Peter Otten __peter__ at web.de
Thu Dec 23 03:32:35 EST 2004


Steven Bethard wrote:

> 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.  The
> code:
> 
> -------------------- remove.py --------------------
> def remove_lc(x, lst):
>      lst[:] = [item for item in lst if item != x]

[snip]

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

I do not think it measures what you think it measures.

A statement in the -s option is only run once, so you have to be careful
with mutable data and pass a copy of your sample to the function about to
be timed. The following example illustrates the problem:

$ py24 -m timeit -n5 -r1 -s"r=[0]" -s"def f(r): print r; r[0] += 1" "f(r)"
[0]
[1]
[2]
[3]
[4]
5 loops, best of 1: 106 usec per loop

Peter




More information about the Python-list mailing list