ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 15 Dec 2011 23:33:23 -0600Why functions are much slower than var in some calculations?https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/Hi,
I am doing symbolic calculations using sage. I want to take derivatives thus I defined some general functions. But I found the functions made other symbolic calculations much more slowly. For example:
a = function('a',t,x,y,z)
var('a1 a2 a3 a4')
# case 1
time matrix(SR, 2, [a,a2,a3,a4]).inverse()
# Time: CPU 0.23 s, Wall: 0.23 s
# case 2
time matrix(SR, 2, [a1,a2,a3,a4]).inverse()
# Time: CPU 0.01 s, Wall: 0.01 s
There is a big time difference simply because in case 1 a is a variable. When considering 4x4 matrices, the difference becomes huge.
I believe there are a few other command slowed down greatly by defining functions, because of the performance difference in my code. I haven't factored out them yet.
Any ideas to improve speed in this case? Thank you very much!Wed, 14 Dec 2011 14:44:55 -0600https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/Answer by tririver for <p>Hi, </p>
<p>I am doing symbolic calculations using sage. I want to take derivatives thus I defined some general functions. But I found the functions made other symbolic calculations much more slowly. For example:</p>
<pre><code>a = function('a',t,x,y,z)
var('a1 a2 a3 a4')
# case 1
time matrix(SR, 2, [a,a2,a3,a4]).inverse()
# Time: CPU 0.23 s, Wall: 0.23 s
# case 2
time matrix(SR, 2, [a1,a2,a3,a4]).inverse()
# Time: CPU 0.01 s, Wall: 0.01 s
</code></pre>
<p>There is a big time difference simply because in case 1 a is a variable. When considering 4x4 matrices, the difference becomes huge. </p>
<p>I believe there are a few other command slowed down greatly by defining functions, because of the performance difference in my code. I haven't factored out them yet.</p>
<p>Any ideas to improve speed in this case? Thank you very much!</p>
https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/?answer=13017#post-id-13017@kcrisman: Thanks a lot for your result!
I am a new sage user thus I still have no idea how A2._echelon_in_place_classical() slows down cython and how to get it improved.
Just to mention, as a work around, now I use a 2d list instead of a matrix object, and hard-cored a formula for inverse matrix to avoid the problem (because I only use 4*4 symmetric matrix now).
On the other hand, it is still extremely interesting to find a solution to this problem :)Thu, 15 Dec 2011 01:09:24 -0600https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/?answer=13017#post-id-13017Answer by kcrisman for <p>Hi, </p>
<p>I am doing symbolic calculations using sage. I want to take derivatives thus I defined some general functions. But I found the functions made other symbolic calculations much more slowly. For example:</p>
<pre><code>a = function('a',t,x,y,z)
var('a1 a2 a3 a4')
# case 1
time matrix(SR, 2, [a,a2,a3,a4]).inverse()
# Time: CPU 0.23 s, Wall: 0.23 s
# case 2
time matrix(SR, 2, [a1,a2,a3,a4]).inverse()
# Time: CPU 0.01 s, Wall: 0.01 s
</code></pre>
<p>There is a big time difference simply because in case 1 a is a variable. When considering 4x4 matrices, the difference becomes huge. </p>
<p>I believe there are a few other command slowed down greatly by defining functions, because of the performance difference in my code. I haven't factored out them yet.</p>
<p>Any ideas to improve speed in this case? Thank you very much!</p>
https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/?answer=13015#post-id-13015Huh, that's weird. I tried it in several different ways and this is consistent.
I can tell you where the bottleneck is. It's in `_echelon_in_place_classical`. I called your matrices `M` and `N`, and:
sage: A1 = M.augment(M.parent().identity_matrix())
sage: time A1 = M.augment(M.parent().identity_matrix())
Time: CPU 0.00 s, Wall: 0.00 s
sage: time A2 = N.augment(N.parent().identity_matrix())
Time: CPU 0.00 s, Wall: 0.00 s
sage: time A1._echelon_in_place_classical()
Time: CPU 0.00 s, Wall: 0.01 s
sage: time A2._echelon_in_place_classical()
Time: CPU 0.24 s, Wall: 0.24 s
My (uninformed) guess is that there is a lot more Python that needs to be used with a "function" than with a "variable", slowing down the Cython. Anyone else?Wed, 14 Dec 2011 15:23:35 -0600https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/?answer=13015#post-id-13015Comment by Volker Braun for <p>Huh, that's weird. I tried it in several different ways and this is consistent.</p>
<p>I can tell you where the bottleneck is. It's in <code>_echelon_in_place_classical</code>. I called your matrices <code>M</code> and <code>N</code>, and:</p>
<pre><code>sage: A1 = M.augment(M.parent().identity_matrix())
sage: time A1 = M.augment(M.parent().identity_matrix())
Time: CPU 0.00 s, Wall: 0.00 s
sage: time A2 = N.augment(N.parent().identity_matrix())
Time: CPU 0.00 s, Wall: 0.00 s
sage: time A1._echelon_in_place_classical()
Time: CPU 0.00 s, Wall: 0.01 s
sage: time A2._echelon_in_place_classical()
Time: CPU 0.24 s, Wall: 0.24 s
</code></pre>
<p>My (uninformed) guess is that there is a lot more Python that needs to be used with a "function" than with a "variable", slowing down the Cython. Anyone else?</p>
https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/?comment=20703#post-id-20703My guess would be that the code tries to simplify the symbolic expression at one point, which means calling maxima via a pexpect interface. This is going to be much slower than just flinging the variables around.Thu, 15 Dec 2011 23:33:23 -0600https://ask.sagemath.org/question/8556/why-functions-are-much-slower-than-var-in-some-calculations/?comment=20703#post-id-20703