Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Well... the %time and %%time directives can be used tp display the execution time a=of an instruction or a cell. See %time. A controlled-precision version s available through the function timeit.

If you want to use this time of execution, yo have to time it yourself. One possibility :

def A1(l, s=1): # First algorithm, defined as a function for laziness' sake
    L=list(range(0,l,s))
    Ld0=[x^2 for x in L]
    Lc=[]
    a=0
    for i in range(len(Ld0)):
        if (Ld0[i]%6 == 0) & (Ld0[i]<= 3200) :
            Lc.append(Ld0[i])
            a+=1
    return Lc
def A2(l, s=1): # Second algorithm (ditto...)
    L=list(range(0,100,10))
    Ld0=[x^2 for x in L]
    Ld2=[x for x in Ld0 if (x%6 == 0) & (x<= 3200)]
    return Ld2
from time import time as stime # See Python documentation
t0 = stime()
R1 = A1(100, 10)
t1 = stime()
R2 = A2(100, 10)
t2 = stime()
Res = [(R1, t1-t0), (R2, t2-t1)] # Wrap results with execution time

which allows you to see thta the firs version is

sage: Res[0][1]/Res[1][1]
6.241007194244604

about 6 times slower than the second.

HTH,

Well... the %time and %%time directives magics can be used tp to display the execution time a=of of an instruction or a cell. See %time. A controlled-precision version s available through the function timeit.

If you want to use this time of execution, yo you have to time it yourself. One possibility :

def A1(l, s=1): # First algorithm, defined as a function for laziness' sake
    L=list(range(0,l,s))
    Ld0=[x^2 for x in L]
    Lc=[]
    a=0
    for i in range(len(Ld0)):
        if (Ld0[i]%6 == 0) & (Ld0[i]<= 3200) :
            Lc.append(Ld0[i])
            a+=1
    return Lc
def A2(l, s=1): # Second algorithm (ditto...)
    L=list(range(0,100,10))
    Ld0=[x^2 for x in L]
    Ld2=[x for x in Ld0 if (x%6 == 0) & (x<= 3200)]
    return Ld2
from time import time as stime # See Python documentation
t0 = stime()
R1 = A1(100, 10)
t1 = stime()
R2 = A2(100, 10)
t2 = stime()
Res = [(R1, t1-t0), (R2, t2-t1)] # Wrap results with execution time

which allows you to see thta the firs that the first version is

sage: Res[0][1]/Res[1][1]
6.241007194244604

about 6 times slower than the second.

HTH,