On Mon, Oct 4, 2010 at 10:21 PM, Rolandb <[email protected]> wrote:
> Hi,
>
> I wonder why this (simple) example is still slow?
>
> %cython
> from sage.all import QQ
>
> def ctel2(int diep):
>    R = QQ['A, B']
>    (A, B) = R._first_ngens(2)
>    cdef long tel=0
>    cdef int i,k0,k1,k2,k3,k4,k5
>    para=[1,A,B,A**2,A*B,B**2]
>    ABC=A*B*(A+B)
>    for k0 in xrange(-diep,diep+1):
>        for k1 in xrange(-diep,diep+1):
>            for k2 in xrange(-diep,diep+1):
>                for k3 in xrange(-diep,diep+1):
>                    for k4 in xrange(-diep,diep+1):
>                        for k5 in xrange(-diep,diep+1):
>                            k=[k0,k1,k2,k3,k4,k5]
>                            if not all([k[i]<=0 for i in xrange(6)]):
>                                pol=0
>                                for i in xrange(6): pol+=k[i]*para[i]
>                                if pol.gcd(ABC)==1: tel+=1
>    return tel
>
> time ctel2(2)
> 14629
> Time: CPU 0.48 s, Wall: 1.51 s
>
> The non-Cython version yields CPU time: 1.90 s,  Wall time: 1.96 s.
>
> I tried to modify the last few lines of code, but that didn't help
> much. It seems that
>    R = QQ['A, B']
>    (A, B) = R._first_ngens(2)
> is the cause of the problem?

Huh? That gets executed once, and will probably take no time at all.

> Any suggestions? Thanks in advance!

Cython itself doesn't do anything to speed up symbolics. If you unwrap
the if statement to be "if k1 > 0 or k2 > 0 ..." and then create the
list k inside the loop, that'd may speed up things a bit, but I bet
most of your time is sitting in your polynomial construction and GCD.
It's all just speculation without a little profiling.

- Robert

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