On Sep 26, 2008, at 12:09 AM, Simon King wrote: > > On Sep 25, 6:45 pm, cesarnda <[EMAIL PROTECTED]> wrote: > ... >> result = [] > ... >> result.append(n) > > ... >> if I compile in the notebook I get a html file showing me the >> following lines in yellow: >> >> def primes(int kmax): >> result = [] >> result.append(n) >> return result >> >> how can I modify this example to avoid the yellow lines? > > As other people pointed out, there is not much hope for the "def" and > the "return" line. > > But wouldn't it be possible to do > cdef list result = []
Yep, you could do this. The resulting C code calls PyList_New(0) which is the fastest way to make a list. > And isn't there a quick, dirty and potentially unsafe way of appending > to a list? Since it knows result is a list (due to the cdef above), it uses PyList_Append, the fastest way to append to a list. > Or at least for assigning a value to some list entry? I > think I have seen it somewhere, but I don't remember the name. Yep. Currently it calls __Pyx_SetItemInt which does an inline runtime boundscheck and check for a list, and if it's OK uses a macro to reset the specified entry right there. We could (should) optimize this in the case it already knows it's a list, but with good branch prediction it's probably within 5% of as fast as it could be period. - Robert --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---