Hi Robert,

On 20 Okt., 06:59, Robert Bradshaw <[email protected]>
wrote:
> I have no idea why L.pop(0) is not optimized, but L.pop(<int>0) is
> (and the latter should be very fast). Clearly a bug in Cython, but
> user the latter.

Surprise: Even though the annotated code for L.pop(<int>0) is less
yellow than for L.pop(0), it is *slower* by about 30%!

In flask.sagenb, I get:
{{{
%cython
def f(list L):
    L.append(1)
    return L.pop(0),L
def g(list L):
    L.append(1)
    return L.pop(<int>0),L
def h(list L):
    L.append(1)
    return L.pop(),L
}}}
{{{
L = range(1000)
}}}

timeit("f(L)")  --> 625 loops, best of 3: 701 ns per loop
timeit("g(L)") --> 625 loops, best of 3: 971 ns per loop
timeit("h(L)") --> 625 loops, best of 3: 131 ns per loop

Since L.pop() (popping the last item) is so much faster than L.pop(0),
I could perhaps just revert my lists.

> > Nevertheless, I'd still appreciate to learn a Cython replacement for
> > pop(0) that is fast on long lists as well.
>
> There isn't one as pop(0) on a list requires re-copying the entire
> contents of the list.

Really? I'm surprised that an in-place change requires copying.

> You could use numpy arrays, where slices such as
> L[1:] are views. A cython-implemented linked list may perform well
> here as well.

I've  never used numpy before, but I'll give it a try.

Thank you very much,
Simon

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to