Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Lopps get slower as they run

Hello, I am trying to run a simulation in sage as part of my homework. The simulation itself is pretty fast and working fine. The problem starts when i try to run the simulation several times in a loop, then sage becomes incredibly slow in each repeat.

The program I wrote:

def poisson(lmu):
var('mu n')
g(mu,n)=(e^-mu * mu^n)/factorial(n)
num=random()
sum=g(lmu,0)

for i in xrange(1000):
    if num<sum:
        return i-1
    else:
        sum=sum+g(lmu,i)

def galtonwatson(mu,s,gens): deaths=0 for i in range (gens): N=1 n=0 while n

        n=n+1
    if N==0:
        deaths=deaths+1
return (deaths/gens).N()

both of the functions works fine, then telling sage to fill a list of 10 arrays with the values, and checking how much time it took for each repeat:

L=np.array([[0,0]for i in range(9)])
for i in xrange (9):
    t=time.time()
    print i
    L[i]= (i,galtonwatson(2,i,100)) 
    print (time.time()-t)

Sage output is:

0

0.000202894210815 1 0.393799066544 2 0.893945932388 3 1.54169392586 4 2.87310600281 5 4.84653711319 6 9.23449707031 7 18.3007540703 8 35.5397469997

It ain't the first time that sage loops becomes slower and slower on the run, and I could'nt understand the reason when searching the net.

Thanks in advanced, David

click to hide/show revision 2
No.2 Revision

Lopps Loops get slower as they run

Hello, I am trying to run a simulation in sage as part of my homework. The simulation itself is pretty fast and working fine. The problem starts when i try to run the simulation several times in a loop, then sage becomes incredibly slow in each repeat.

The program I wrote:

def poisson(lmu):
var('mu n')
g(mu,n)=(e^-mu * mu^n)/factorial(n)
num=random()
sum=g(lmu,0)

for i in xrange(1000):
    if num<sum:
        return i-1
    else:
        sum=sum+g(lmu,i)

def galtonwatson(mu,s,gens): deaths=0 for i in range (gens): N=1 n=0 while n

        n=n+1
    if N==0:
        deaths=deaths+1
return (deaths/gens).N()

both of the functions works fine, then telling sage to fill a list of 10 arrays with the values, and checking how much time it took for each repeat:

L=np.array([[0,0]for i in range(9)])
for i in xrange (9):
    t=time.time()
    print i
    L[i]= (i,galtonwatson(2,i,100)) 
    print (time.time()-t)

Sage output is:

0

0.000202894210815 1 0.393799066544 2 0.893945932388 3 1.54169392586 4 2.87310600281 5 4.84653711319 6 9.23449707031 7 18.3007540703 8 35.5397469997

It ain't the first time that sage loops becomes slower and slower on the run, and I could'nt understand the reason when searching the net.

Thanks in advanced, David

click to hide/show revision 3
No.3 Revision

Loops get slower as they run

Hello, I am trying to run a simulation in sage as part of my homework. The simulation itself is pretty fast and working fine. The problem starts when i try to run the simulation several times in a loop, then sage becomes incredibly slow in each repeat.

The program I wrote:

def poisson(lmu):
var('mu n')
g(mu,n)=(e^-mu * mu^n)/factorial(n)
num=random()
sum=g(lmu,0)

for i in xrange(1000):
    if num<sum:
        return i-1
    else:
        sum=sum+g(lmu,i)

def galtonwatson(mu,s,gens): deaths=0 for i in range (gens): N=1 n=0 while n

n<s and N!=0:
            N=poisson(N*mu)

            n=n+1
     if N==0:
         deaths=deaths+1
 return (deaths/gens).N()

both of the functions works fine, then telling sage to fill a list of 10 arrays with the values, and checking how much time it took for each repeat:

L=np.array([[0,0]for i in range(9)])
for i in xrange (9):
    t=time.time()
    print i
    L[i]= (i,galtonwatson(2,i,100)) 
    print (time.time()-t)

Sage output is:

0

0.000202894210815 1 0.393799066544 2 0.893945932388 3 1.54169392586 4 2.87310600281 5 4.84653711319 6 9.23449707031 7 18.3007540703 8 35.5397469997

35.5397469997

It ain't the first time that sage loops becomes slower and slower on the run, and I could'nt understand the reason when searching the net.

Thanks in advanced, David

Loops get slower as they run

Hello, I am trying to run a simulation in sage as part of my homework. The simulation itself is pretty fast and working fine. The problem starts when i try to run the simulation several times in a loop, then sage becomes incredibly slow in each repeat.

The program I wrote:

def poisson(lmu):
 var('mu n')
 g(mu,n)=(e^-mu * mu^n)/factorial(n)
 num=random()
 sum=g(lmu,0)

 for i in xrange(1000):
     if num<sum:
         return i-1
     else:
         sum=sum+g(lmu,i)

def galtonwatson(mu,s,gens):
    deaths=0
    for i in range (gens):
        N=1
        n=0
        while n<s and N!=0:
            N=poisson(N*mu)

            n=n+1
        if N==0:
            deaths=deaths+1
    return (deaths/gens).N()

both of the functions works fine, then telling sage to fill a list of 10 arrays with the values, and checking how much time it took for each repeat:

L=np.array([[0,0]for i in range(9)])
for i in xrange (9):
    t=time.time()
    print i
    L[i]= (i,galtonwatson(2,i,100)) 
    print (time.time()-t)

Sage output is:

0
0.000202894210815
1
0.393799066544
2
0.893945932388
3
1.54169392586
4
2.87310600281
5
4.84653711319
6
9.23449707031
7
18.3007540703
8
35.5397469997

It ain't the first time that sage loops becomes slower and slower on the run, and I could'nt understand the reason when searching the net.

Thanks in advanced, David