ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 19 Feb 2023 03:12:32 +0100Comparing the computational time of two ways of the same resulthttps://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/The two following codes accomplish the same task
1)
L=list(range(0,100,10))
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
2)
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)]
which can be shown by `Lc==ld2`.
If I can fin easily the cpu computation time in adding `%time` to the first, this is not the case for the second. How to see which way is the fastest ?
Sat, 18 Feb 2023 09:00:21 +0100https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/Comment by Max Alekseyev for <p>The two following codes accomplish the same task</p>
<p>1)</p>
<pre><code>L=list(range(0,100,10))
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
</code></pre>
<p>2)</p>
<pre><code>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)]
</code></pre>
<p>which can be shown by <code>Lc==ld2</code>.</p>
<p>If I can fin easily the cpu computation time in adding <code>%time</code> to the first, this is not the case for the second. How to see which way is the fastest ?</p>
https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66505#post-id-66505If there is no obvious winner, you can run the two algorithms in parallel, and stop computation as soon as one produces the result - see https://ask.sagemath.org/question/59480/Sat, 18 Feb 2023 14:16:04 +0100https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66505#post-id-66505Comment by FrédéricC for <p>The two following codes accomplish the same task</p>
<p>1)</p>
<pre><code>L=list(range(0,100,10))
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
</code></pre>
<p>2)</p>
<pre><code>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)]
</code></pre>
<p>which can be shown by <code>Lc==ld2</code>.</p>
<p>If I can fin easily the cpu computation time in adding <code>%time</code> to the first, this is not the case for the second. How to see which way is the fastest ?</p>
https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66500#post-id-66500On peut virer le `list` dans la première ligne. et utiliser un itérateur dans la deuxième.Sat, 18 Feb 2023 09:27:28 +0100https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66500#post-id-66500Answer by Emmanuel Charpentier for <p>The two following codes accomplish the same task</p>
<p>1)</p>
<pre><code>L=list(range(0,100,10))
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
</code></pre>
<p>2)</p>
<pre><code>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)]
</code></pre>
<p>which can be shown by <code>Lc==ld2</code>.</p>
<p>If I can fin easily the cpu computation time in adding <code>%time</code> to the first, this is not the case for the second. How to see which way is the fastest ?</p>
https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?answer=66502#post-id-66502Well... the `%time` and `%%time` [magic](https://doc.sagemath.org/html/en/reference/repl/sage/repl/interface_magic.html)s can be used to *display* the execution time 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, 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 that the first version is
sage: Res[0][1]/Res[1][1]
6.241007194244604
about 6 times slower than the second.
HTH,Sat, 18 Feb 2023 12:08:42 +0100https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?answer=66502#post-id-66502Comment by achrzesz for <p>Well... the <code>%time</code> and <code>%%time</code> <a href="https://doc.sagemath.org/html/en/reference/repl/sage/repl/interface_magic.html">magic</a>s can be used to <em>display</em> the execution time of an instruction or a cell. See <code>%time</code>. A controlled-precision version s available through the function <code>timeit</code>.</p>
<p>If you want to <em>use</em> this time of execution, you have to time it yourself. One possibility :</p>
<pre><code>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
</code></pre>
<p>which allows you to see that the first version is </p>
<pre><code>sage: Res[0][1]/Res[1][1]
6.241007194244604
</code></pre>
<p>about 6 times slower than the second.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66504#post-id-66504In the definition of A2, the second line should be probably L=range(0,l,s) ?Sat, 18 Feb 2023 14:01:56 +0100https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66504#post-id-66504Comment by John Palmieri for <p>Well... the <code>%time</code> and <code>%%time</code> <a href="https://doc.sagemath.org/html/en/reference/repl/sage/repl/interface_magic.html">magic</a>s can be used to <em>display</em> the execution time of an instruction or a cell. See <code>%time</code>. A controlled-precision version s available through the function <code>timeit</code>.</p>
<p>If you want to <em>use</em> this time of execution, you have to time it yourself. One possibility :</p>
<pre><code>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
</code></pre>
<p>which allows you to see that the first version is </p>
<pre><code>sage: Res[0][1]/Res[1][1]
6.241007194244604
</code></pre>
<p>about 6 times slower than the second.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66516#post-id-66516If you will be iterating, using ‘range(…)’ is faster than ‘list(range(…))’. Only convert to a list if you really need all of the elements at once.Sun, 19 Feb 2023 03:12:32 +0100https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66516#post-id-66516Comment by Cyrille for <p>Well... the <code>%time</code> and <code>%%time</code> <a href="https://doc.sagemath.org/html/en/reference/repl/sage/repl/interface_magic.html">magic</a>s can be used to <em>display</em> the execution time of an instruction or a cell. See <code>%time</code>. A controlled-precision version s available through the function <code>timeit</code>.</p>
<p>If you want to <em>use</em> this time of execution, you have to time it yourself. One possibility :</p>
<pre><code>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
</code></pre>
<p>which allows you to see that the first version is </p>
<pre><code>sage: Res[0][1]/Res[1][1]
6.241007194244604
</code></pre>
<p>about 6 times slower than the second.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66503#post-id-66503Thanks Emmanuel. Should I say that a the second method is always more efficient that the first, that is working directly inside a list is better than to construct a loop (I know that list comprehension is recursive and has been optimized)?Sat, 18 Feb 2023 12:54:10 +0100https://ask.sagemath.org/question/66497/comparing-the-computational-time-of-two-ways-of-the-same-result/?comment=66503#post-id-66503