fast sub list operations

mwh at python.net mwh at python.net
Wed Oct 17 06:15:48 EDT 2001


Robin Becker <robin at jessikat.fsnet.co.uk> writes:

> I would expect that there should at least be a way to split lists
> using ranges and I know that this is done in NumPy so why not in
> python.

I actually implemented this for Python a few months back, but it was
confusing in some case (e.g.  negative strides) and it got rejected.
You can find the patch on sf, if you care.

> map and reduce etc are fast, but useless without builtin C speed
> things to go with them.

Hmm; Python is odd here.  If you're iterating over a list and doing
simple things to it, it's probably faster to just write it in Python:
for example interleave2 is faster (and *lots* faster for bigger
input[0]) than interleave1 (on my machine, on this day of the week,
etc.):

def interleave1(l1,l2,add=add):
    return reduce(add,zip(l1,l2),())

def interleave2(l1,l2):
    r = []
    for i in range(len(l1)):
        r.append(l1[i])
        r.append(l2[i])
    return r

list[int] is optimised in the eval loop, whereas the former probably
has to go through the full getitem machinery *and* the function call
machinery to call operator.add.

profile-profile-profile-ly y'rs
M.

[0] Actually, this is obvious.  interleave1 is quadratic in list
length, so maybe that's what's killing it, rather than the eval loop
optimisations.  Remind me what reduce is doing in the language again?

-- 
  Remember - if all you have is an axe, every problem looks 
  like hours of fun.                                        -- Frossie
               -- http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html



More information about the Python-list mailing list