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

Peter Hansen peter at engcorp.com
Fri May 21 12:32:37 EDT 2004


Noam Raphael wrote:

> Peter Hansen wrote:
> 
>> Noam Raphael wrote:
>>
>>> Grant Edwards wrote:
>>>
>>>> It's a bit less efficient, but you can currently spell that as
>>>>
>>>> l[12345:][:10]
>>>>
>>> That is true, but if the list is long, it's *much* less efficient.
>>
>>
>>
>> Considering that the interpreter special-cases some integer math
>> including the BINARY_ADD, it likely wouldn't take a very long list
>> to pass the point where they're the same.
>>
> 
> I don't understand: If the list is of length 1000000, wouldn't Grant 
> Edwards' suggestion make 1000000-12345 new references, and then take 
> only the first ten of them?

Sorry, it was perhaps unclear that I was agreeing with you.  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,
etc.

-Peter



More information about the Python-list mailing list