Tuple slices

Nick Coghlan ncoghlan at iinet.net.au
Wed Jan 26 06:04:05 EST 2005

jfj wrote:
> Jeff Shannon wrote:
>> So, what problem is it, exactly, that you think you'd solve by making 
>> tuple slices a view rather than a copy?
> I think views are good for
>  1) saving memory
>  2) saving time (as you don't have to copy the elements into the new tuple)

1. Applies only if you are making large slices, or a lot of slices with each 
containing at least 3 elements.
   A view can also *cost* memory, when it looks at a small piece of a large 
item. The view will keep the entire item alive, even though it needs only a 
small piece.

2. Hell no. The *elements* aren't copied, pointers to the elements are. If you 
*don't* copy the pointers, then every item access through the view involves an 
indirection as the index into the original sequence gets calculated.

So views *may* save memory in some applications, but are unlikely to save time 
in any application (except any saving resulting from the memory saving).


Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia

More information about the Python-list mailing list