Hello,
This is my first message to this list, but I hope, that I have not
broken any rules here. I am writing because I have noticed a strange
thing, which I think should not happen.
I needed to test my maths solutions, so I thought I would just use a
loop to get sage generate the first 600 terms of a particular Fourier
series. But then I remembered that sage has a sum function. So I also
tried the sum function. However, I noticed a very big performance
difference which I did not expect. I was wondering if someone could
explain me why there is such a difference in the performance although
the actual result is the same? Or is the native sum function more clever
or what?
The output of the script and the script itself are attached. Note that I
did not bother to wait for the native sum function to sum 1000 terms, as
it took eternity to finish.
Cheers,
Ignas A.
--
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
sage: load('script.sage')
Comparison with 10 terms
Loop function:
7 loops, best of 3: 3.168 ms per loop
Native sum function:
7 loops, best of 3: 245.3 ms per loop
Comparison with 100 terms
Loop function:
7 loops, best of 3: 33.63 ms per loop
Native sum function:
7 loops, best of 3: 1.952 s per loop
Comparison with 1000 terms
Loop function:
7 loops, best of 3: 555.1 ms per loop
Native sum function:
^C---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/gns-ank/<ipython console> in <module>()
/usr/lib64/python2.7/site-packages/sage/structure/sage_object.so in
sage.structure.sage_object.load (sage/structure/sage_object.c:7368)()
/usr/lib64/python2.7/site-packages/sage/misc/preparser.pyc in load(filename,
globals, attach)
1594 execfile(fpath, globals)
1595 elif fpath.endswith('.sage'):
-> 1596 exec(preparse_file(open(fpath).read()) + "\n", globals)
1597 elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
1598 import interpreter
/home/gns-ank/<string> in <module>()
/usr/lib64/python2.7/site-packages/sage/misc/sage_timeit_class.so in
sage.misc.sage_timeit_class.SageTimeit.__call__
(sage/misc/sage_timeit_class.c:744)()
/usr/lib64/python2.7/site-packages/sage/misc/sage_timeit_class.so in
sage.misc.sage_timeit_class.SageTimeit.eval
(sage/misc/sage_timeit_class.c:605)()
/usr/lib64/python2.7/site-packages/sage/misc/sage_timeit.pyc in
sage_timeit(stmt, globals, preparse, number, repeat, precision)
182 break
183
--> 184 best = min(timer.repeat(repeat, number)) / number
185
186 finally:
/usr/lib64/python2.7/timeit.pyc in repeat(self, repeat, number)
219 r = []
220 for i in range(repeat):
--> 221 t = self.timeit(number)
222 r.append(t)
223 return r
/usr/lib64/python2.7/timeit.pyc in timeit(self, number)
192 gcold = gc.isenabled()
193 gc.disable()
--> 194 timing = self.inner(it, self.timer)
195 if gcold:
196 gc.enable()
/home/gns-ank/<magic-timeit> in inner(_it, _timer)
/home/gns-ank/<string> in fsum(x, a)
/usr/lib64/python2.7/site-packages/sage/misc/functional.pyc in
symbolic_sum(expression, *args, **kwds)
657 """
658 if hasattr(expression, 'sum'):
--> 659 return expression.sum(*args, **kwds)
660 elif len(args) <= 1:
661 return sum(expression, *args)
/usr/lib64/python2.7/site-packages/sage/symbolic/expression.so in
sage.symbolic.expression.Expression.sum (sage/symbolic/expression.cpp:31223)()
/usr/lib64/python2.7/site-packages/sage/calculus/calculus.pyc in
symbolic_sum(expression, v, a, b, algorithm)
533 sum = "'sum(%s, %s, %s, %s)" % tuple([repr(expr._maxima_())
for expr in (expression, v, a, b)])
534 try:
--> 535 result = maxima.simplify_sum(sum)
536 result = result.ratsimp()
537 return expression.parent()(result)
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in __call__(self,
*args, **kwds)
1466
1467 def __call__(self, *args, **kwds):
-> 1468 return self._parent.function_call(self._name, list(args), kwds)
1469
1470 def _sage_doc_(self):
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in
function_call(self, function, args, kwds)
1392 [s.name() for s in args],
1393 ['%s=%s'%(key,value.name()) for
key, value in kwds.items()])
-> 1394 return self.new(s)
1395
1396 def _function_call_string(self, function, args, kwds):
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in new(self, code)
1167
1168 def new(self, code):
-> 1169 return self(code)
1170
1171 ###################################################################
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in __call__(self,
x, name)
1103
1104 if isinstance(x, basestring):
-> 1105 return cls(self, x, name=name)
1106 try:
1107 return self._coerce_from_special_method(x)
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in __init__(self,
parent, value, is_name, name)
1536 except (TypeError, KeyboardInterrupt, RuntimeError,
ValueError), x:
1537 self._session_number = -1
-> 1538 raise TypeError, x
1539 self._session_number = parent._session_number
1540
TypeError:
x,n = var('x n')
def floop(x,a):
g(x) = 1/2
for n in range(1,a+1):
g += 2*(((-1)^n-1)/(n*pi)^2*cos(n*pi*x) -
(-1)^n/(n*pi)*sin(n*pi*x))
return g
def fsum(x,a):
g(x) = 1/2
g += sum(2*(((-1)^n-1)/(n*pi)^2*cos(n*pi*x) -
(-1)^n/(n*pi)*sin(n*pi*x)),n,1,a)
return g
print "Comparison with 10 terms"
print "Loop function:"
timeit('g(x) = floop(x,10)', preparse=None, number=7, precision = 4)
print "Native sum function:"
timeit('f(x) = fsum(x,10)', preparse=None, number=7, precision = 4)
print "Comparison with 100 terms"
print "Loop function:"
timeit('g(x) = floop(x,100)', preparse=None, number=7, precision = 4)
print "Native sum function:"
timeit('f(x) = fsum(x,100)', preparse=None, number=7, precision = 4)
print "Comparison with 1000 terms"
print "Loop function:"
timeit('g(x) = floop(x,1000)', preparse=None, number=7, precision = 4)
print "Native sum function:"
timeit('f(x) = fsum(x,1000)', preparse=None, number=7, precision = 4)