Specify start and length, beside start and end, in slices

Peter Hansen peter at engcorp.com
Fri May 21 13:30:03 EDT 2004


Peter Hansen wrote:

> For an extremely short list, it's possible that it would be faster
> to do Grant's method, but what I was trying to say is that even
> if that's true, I expect that for a list of more than a few dozen
> elements it would not be faster.  Looking at it again, I suspect
> that it would actually never be faster, given that probably
> about as many bytecode instructions are executed, and then there's
> the extra memory allocation for the temporary list, the copying,

timeit confirms this with variations on this:

c:\>python -c "import timeit as t; t = t.Timer('x[y:][:10]', 'y=10000; 
x=range(y)'); print t.timeit()"

and this:

c:\>python -c "import timeit as t; t = t.Timer('x[y:y+10]', 'y=10000; 
x=range(y)'); print t.timeit()"

-Peter



More information about the Python-list mailing list