Re: [sage-support] arrays in Sage's cython
On Thursday, August 22, 2013 6:21:13 AM UTC-7, John Cremona wrote: > > Then use srange() which yields Integers. > > John > The appropriate answer should be: use xsrange, unless you explicitly need the integers as a list. The extra memory footprint of srange will probably be detrimental to performance. except that it isn't uniformly the case: sage: timeit("for i in xsrange(100): a=i") 5 loops, best of 3: 157 ms per loop sage: timeit("for i in srange(100): a=i") 5 loops, best of 3: 179 ms per loop sage: timeit("for i in xsrange(1000): a=i") 625 loops, best of 3: 226 µs per loop sage: timeit("for i in srange(1000): a=i") 625 loops, best of 3: 219 µs per loop so perhaps with a little more careful programming (cythonizing xsrange?) one might be able to get the appropriate advice to be the uniformly correct advice as well. I haven't timed it, but there's a good chance that writing the looping in cython to use a straight C-int and converting that (once) to an Integer, or using the while loop, is faster than deal with the overhead from (x)srange, even if they were optimized. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sage-support] arrays in Sage's cython
On 22 August 2013 14:10, Daniel Krenn wrote: > Am 2013-08-22 01:12, schrieb Robert Bradshaw: >> Using a Python list is probably the fastest way to iterate over an >> array of Python objects--it's a PyObject** under the hood and Cython >> uses the C API calls to get at it. > > Ok, thanks for the clearification. > >> Your "check" might be the >> bottleneck, especially if it's a Python call. > > I could remove that bottleneck. Now the bottleneck is in my original > "something". I have to live with that. But, indeed, the list was not the > problem. Thanks. > >> Also, no need to write this as a while loop; just use "for a in >> range(100)" and it'll do the right thing (a C for loop). > > I used the while loop, since I want a to be Integer and not int (I need > that for the calculations). Then use srange() which yields Integers. John > > Many thanks, > > Daniel > > > -- > You received this message because you are subscribed to the Google Groups > "sage-support" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-support+unsubscr...@googlegroups.com. > To post to this group, send email to sage-support@googlegroups.com. > Visit this group at http://groups.google.com/group/sage-support. > For more options, visit https://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sage-support] arrays in Sage's cython
Am 2013-08-22 01:12, schrieb Robert Bradshaw: > Using a Python list is probably the fastest way to iterate over an > array of Python objects--it's a PyObject** under the hood and Cython > uses the C API calls to get at it. Ok, thanks for the clearification. > Your "check" might be the > bottleneck, especially if it's a Python call. I could remove that bottleneck. Now the bottleneck is in my original "something". I have to live with that. But, indeed, the list was not the problem. Thanks. > Also, no need to write this as a while loop; just use "for a in > range(100)" and it'll do the right thing (a C for loop). I used the while loop, since I want a to be Integer and not int (I need that for the calculations). Many thanks, Daniel -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sage-support] arrays in Sage's cython
Using a Python list is probably the fastest way to iterate over an array of Python objects--it's a PyObject** under the hood and Cython uses the C API calls to get at it. Your "check" might be the bottleneck, especially if it's a Python call. Also, no need to write this as a while loop; just use "for a in range(100)" and it'll do the right thing (a C for loop). On Wed, Aug 21, 2013 at 12:33 PM, Daniel Krenn wrote: > I need an array of Elements of RealIntervalField and I want to iterate > (a lot of times) through it. How can I do that in a fast way in Sage's > cython (i.e. in the notebook or in a .spyx-file)? > > An (minimal) example what I basically want is given below (PS). > > I tried the following things (and a lot more) in the Sage notebook as > well as in a .spyx-file: > > > 1. using vector > > from libcpp cimport vector > > gives > > filename.c:317:18: fatal error: vector: file or directory not found > compilation terminated. > error: command 'gcc' failed with exit status 1 > > At least I found the file in Cython/include/... > > > 2. using C arrays > > cimport sage.rings.real_mpfi # do not know whether that is needed... > from sage.rings.real_mpfi cimport RealIntervalFieldElement > cdef RealIntervalFieldElement L[10] > > says > > cdef RealIntervalFieldElement L[10] > ^ > filename.pyx:12:31: Array element cannot be a Python object > > I thought that with cimport I get a C/cython object (but maybe I have > too less understanding of it) > > > 3. using some pointers > > from sage.rings.real_mpfi cimport RealIntervalFieldElement > from sage.all import RIF > cdef void* L[10] > cdef RealIntervalFieldElement r = RIF(100) > L[0] = &r > > gives > > cdef RealIntervalFieldElement r = RIF(100) > L[0] = &r ^ > filename.pyx:15:7: Cannot take address of Python variable > > > I am thankful for any input. > > Daniel > > > PS: Here is what I want faster: > > L = [blabla] # a python list of length about 500 > a=0 > while a<1000: > b = something(a) > for ell in L: > if check(a,b,ell): > print "yeah" # or do whatever > a += 1 > > L is a list of RealIntervalField elements; b is a RealIntervalElement, too. > > -- > You received this message because you are subscribed to the Google Groups > "sage-support" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-support+unsubscr...@googlegroups.com. > To post to this group, send email to sage-support@googlegroups.com. > Visit this group at http://groups.google.com/group/sage-support. > For more options, visit https://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/groups/opt_out.
[sage-support] arrays in Sage's cython
I need an array of Elements of RealIntervalField and I want to iterate (a lot of times) through it. How can I do that in a fast way in Sage's cython (i.e. in the notebook or in a .spyx-file)? An (minimal) example what I basically want is given below (PS). I tried the following things (and a lot more) in the Sage notebook as well as in a .spyx-file: 1. using vector from libcpp cimport vector gives filename.c:317:18: fatal error: vector: file or directory not found compilation terminated. error: command 'gcc' failed with exit status 1 At least I found the file in Cython/include/... 2. using C arrays cimport sage.rings.real_mpfi # do not know whether that is needed... from sage.rings.real_mpfi cimport RealIntervalFieldElement cdef RealIntervalFieldElement L[10] says cdef RealIntervalFieldElement L[10] ^ filename.pyx:12:31: Array element cannot be a Python object I thought that with cimport I get a C/cython object (but maybe I have too less understanding of it) 3. using some pointers from sage.rings.real_mpfi cimport RealIntervalFieldElement from sage.all import RIF cdef void* L[10] cdef RealIntervalFieldElement r = RIF(100) L[0] = &r gives cdef RealIntervalFieldElement r = RIF(100) L[0] = &r ^ filename.pyx:15:7: Cannot take address of Python variable I am thankful for any input. Daniel PS: Here is what I want faster: L = [blabla] # a python list of length about 500 a=0 while a<1000: b = something(a) for ell in L: if check(a,b,ell): print "yeah" # or do whatever a += 1 L is a list of RealIntervalField elements; b is a RealIntervalElement, too. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/groups/opt_out.