[sage-support] Re: lists in cython

2008-09-26 Thread Robert Bradshaw

On Sep 25, 2008, at 7:06 PM, Jason Grout wrote:

 cesarnda wrote:
 why the line:

 def primes(int kmax):

 is in yellow?

It's yellow because it's handling a python function call (parsing the  
arguments, converting a python object to an int, ...)

 If you click on the line, you can see the actual C code that Cython
 generated for you.  Doing that, you'll notice that there are quite  
 a few
 more python calls stemming from yellow lines than the white lines.   
 The
 more yellow a line is, the slower it will probably run because it is
 doing more python stuff than just straight C stuff.  Some python stuff
 is unavoidable, though, like the def statement above.

 Note that the coloring of the lines is heuristic; it's not a guarantee
 that those lines are the ones that are running slowly.  But it does  
 help
 quite a bit if you want to speed up your code: make sure that the  
 inner
 loops don't have very much yellow in them.

Yep. The color of a line is proportional to (or at least an  
increasing function of) the number of times the Python C API is  
invoked. Pure white should be 100% C. As Jason mentioned, it is  
important to note that yellow does not necessarily mean slow, simply  
that there's lots of implicit stuff going on that's often not obvious  
by looking at the code (and is very helpful in caching stuff like  
unnecessary coercions due to forgetting to cdef a variable).

 Personally, I think the coloring of the lines and the ability to click
 on them is one of the neatest features about developing cython in the
 notebook.

Thanks :). From the command line you can run cython -a.

- 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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-26 Thread Simon King

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 = []
?

And isn't there a quick, dirty and potentially unsafe way of appending
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.

Cheers
   Simon

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-26 Thread Robert Bradshaw

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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-26 Thread Robert Bradshaw

On Sep 25, 2008, at 3:45 PM, cesarnda wrote:


 in http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ there is
 the following example:

 def primes(int kmax):
   cdef int n, k, i
   cdef int p[1000]
   result = []
   if kmax  1000:
 kmax = 1000
   k = 0
   n = 2
   while k  kmax:
 i = 0
 while i  k and n % p[i]  0:
   i = i + 1
 if i == k:
   p[k] = n
   k = k + 1
   result.append(n)
 n = n + 1
   return result

 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?

You can't, but they're not very yellow (i.e. there's not really a  
faster way of doing those operations).

- 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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-26 Thread cesarnda

I already did that and I get this:

 cdef list codeSet = []

  __pyx_1 = PyList_New(0); if (unlikely(!__pyx_1)) {__pyx_filename =
__pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto
__pyx_L1_error;}
  __pyx_v_codeSet = __pyx_1;
  __pyx_1 = 0;


and if I don't do it that way I get:

 addVector = []

  __pyx_2 = PyList_New(0); if (unlikely(!__pyx_2))
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno =
__LINE__; goto __pyx_L1_error;}
  Py_DECREF(__pyx_v_addVector);
  __pyx_v_addVector = ((PyObject *)__pyx_2);
  __pyx_2 = 0;


On Sep 26, 2:55 am, Robert Bradshaw [EMAIL PROTECTED]
wrote:
 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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-26 Thread Robert Bradshaw

On Sep 26, 2008, at 8:21 AM, cesarnda wrote:


 I already did that and I get this:

  cdef list codeSet = []

   __pyx_1 = PyList_New(0); if (unlikely(!__pyx_1)) {__pyx_filename =
 __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto
 __pyx_L1_error;}
   __pyx_v_codeSet = __pyx_1;
   __pyx_1 = 0;


 and if I don't do it that way I get:

  addVector = []

   __pyx_2 = PyList_New(0); if (unlikely(!__pyx_2))
 {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno =
 __LINE__; goto __pyx_L1_error;}
   Py_DECREF(__pyx_v_addVector);
   __pyx_v_addVector = ((PyObject *)__pyx_2);
   __pyx_2 = 0;

Yes, that is correct. Is this not what you want?



 On Sep 26, 2:55 am, Robert Bradshaw [EMAIL PROTECTED]
 wrote:
 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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-26 Thread cesarnda

Actually I wanted it less yellow, if I do that or only

codeSet = []

or

codeSet = ([])

the result is the same.

On Sep 26, 1:43 pm, Robert Bradshaw [EMAIL PROTECTED]
wrote:
 On Sep 26, 2008, at 8:21 AM, cesarnda wrote:





  I already did that and I get this:

   cdef list codeSet = []

    __pyx_1 = PyList_New(0); if (unlikely(!__pyx_1)) {__pyx_filename =
  __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto
  __pyx_L1_error;}
    __pyx_v_codeSet = __pyx_1;
    __pyx_1 = 0;

  and if I don't do it that way I get:

   addVector = []

            __pyx_2 = PyList_New(0); if (unlikely(!__pyx_2))
  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno =
  __LINE__; goto __pyx_L1_error;}
            Py_DECREF(__pyx_v_addVector);
            __pyx_v_addVector = ((PyObject *)__pyx_2);
            __pyx_2 = 0;

 Yes, that is correct. Is this not what you want?



  On Sep 26, 2:55 am, Robert Bradshaw [EMAIL PROTECTED]
  wrote:
  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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-26 Thread Robert Bradshaw

On Sep 26, 2008, at 6:04 PM, cesarnda wrote:

 Actually I wanted it less yellow, if I do that or only

 codeSet = []

 or

 codeSet = ([])

 the result is the same.

You can't create a list in a less yellow way than calling PyList_New 
(0), and that's all its doing so you're optimal.

- Robert

 On Sep 26, 1:43 pm, Robert Bradshaw [EMAIL PROTECTED]
 wrote:
 On Sep 26, 2008, at 8:21 AM, cesarnda wrote:





 I already did that and I get this:

  cdef list codeSet = []

   __pyx_1 = PyList_New(0); if (unlikely(!__pyx_1)) {__pyx_filename =
 __pyx_f[0]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto
 __pyx_L1_error;}
   __pyx_v_codeSet = __pyx_1;
   __pyx_1 = 0;

 and if I don't do it that way I get:

  addVector = []

   __pyx_2 = PyList_New(0); if (unlikely(!__pyx_2))
 {__pyx_filename = __pyx_f[0]; __pyx_lineno = 215; __pyx_clineno =
 __LINE__; goto __pyx_L1_error;}
   Py_DECREF(__pyx_v_addVector);
   __pyx_v_addVector = ((PyObject *)__pyx_2);
   __pyx_2 = 0;

 Yes, that is correct. Is this not what you want?



 On Sep 26, 2:55 am, Robert Bradshaw [EMAIL PROTECTED]
 wrote:
 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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-25 Thread cesarnda

why the line:

def primes(int kmax):

is in yellow?

On Sep 25, 8:03 pm, Robert Bradshaw [EMAIL PROTECTED]
wrote:
 On Sep 25, 2008, at 3:45 PM, cesarnda wrote:





  inhttp://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/there is
  the following example:

  def primes(int kmax):
    cdef int n, k, i
    cdef int p[1000]
    result = []
    if kmax  1000:
      kmax = 1000
    k = 0
    n = 2
    while k  kmax:
      i = 0
      while i  k and n % p[i]  0:
        i = i + 1
      if i == k:
        p[k] = n
        k = k + 1
        result.append(n)
      n = n + 1
    return result

  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?

 You can't, but they're not very yellow (i.e. there's not really a  
 faster way of doing those operations).

 - 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
-~--~~~~--~~--~--~---



[sage-support] Re: lists in cython

2008-09-25 Thread Jason Grout

cesarnda wrote:
 why the line:
 
 def primes(int kmax):
 
 is in yellow?


If you click on the line, you can see the actual C code that Cython 
generated for you.  Doing that, you'll notice that there are quite a few 
more python calls stemming from yellow lines than the white lines.  The 
more yellow a line is, the slower it will probably run because it is 
doing more python stuff than just straight C stuff.  Some python stuff 
is unavoidable, though, like the def statement above.

Note that the coloring of the lines is heuristic; it's not a guarantee 
that those lines are the ones that are running slowly.  But it does help 
quite a bit if you want to speed up your code: make sure that the inner 
loops don't have very much yellow in them.

Personally, I think the coloring of the lines and the ability to click 
on them is one of the neatest features about developing cython in the 
notebook.

Jason


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sage-support] Re: #if in cython?

2007-10-13 Thread William Stein

On 10/13/07, Simon King [EMAIL PROTECTED] wrote:

 Dear sage-supporters,

 a question on pyrex/cython:
 I have type definitions depending on an environment variable, such as

 #if ZZZ==zzz
 typedef unsigned char FEL;
 #elif ZZZ==bigzzz
 typedef unsigned short FEL;

 Is there a way to do a similar thing in pyrex/cython?

 Yours sincerely

This question belongs in the cython or pyrex mailing lists,
rather than sage-support, so I've forwarded it there.

William

--~--~-~--~~~---~--~~
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://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: #if in cython?

2007-10-13 Thread mabshoff



On Oct 14, 2:37 am, Simon King [EMAIL PROTECTED] wrote:
 Dear William,

 you wrote:
  This question belongs in the cython or pyrex mailing lists,
  rather than sage-support, so I've forwarded it there.

 After posting my question, i found an answer to my question in the
 web:
 Pyrex 0.9.6 provides conditional compilation. 
 Seehttp://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/...

 (However, i didn't succeed to make it work on some easy examples, so
 it seems i need more information)

 I do think that my question remains a question for sage, since sage -
 cython apparently does not provide that new pyrex functionality. The
 line
 DEF VARI = Value
 is accepted by pyrexc, but not by sage -cython.


There has been a very recent release of pyrex and the changes/updates
from that release haven't made it into Cython's latest release yet (at
least not the one in the official tree)

 Can you tell me the web address of the pyrex or cython mailin lists,
 so that i can read answers?


See http://developer.berlios.de/projects/cython/
 Yours
Simon

Cheers,

Michael


--~--~-~--~~~---~--~~
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://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---