non-copy slices

Daniel Stutzbach daniel at stutzbachenterprises.com
Wed Nov 18 21:47:07 EST 2009


On Wed, Nov 18, 2009 at 2:22 PM, Themis Bourdenas <
t.bourdenas07 at imperial.ac.uk> wrote:

> It's nothing in the library that completely imitates the slice without the
> copies, right?


You might be interested in my blist extension type (link below).
Syntactically, using a blist is just like using a list, but it has different
performance characteristics.

In particular for your needs, taking a slice is cheap.  The blist implements
copy-on-write behind the scenes, so taking a slice takes O(log n) time,
where n is the size of the initial blist.

http://pypi.python.org/pypi/blist/

Here is a simple example, which creates a blist with over 500 million
elements and takes a slice of over 500 million elements.  In under 22
microseconds. :-)

from blist import blist
small_list = blist([0])
BIG_list = small_list * 2**29
BIG_slice = BIG_list[4:-5]

Cashew:~$ python2.5 -m timeit -s 'from blist import blist'
'small_list=blist([0])' ''BIG_list=small_list*2**29'
'BIG_slice=BIG_list[4:-5]'
10000 loops, best of 3: 21.5 usec per loop

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091118/6ed96bfe/attachment-0001.html>


More information about the Python-list mailing list