ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 05 Mar 2013 14:21:49 -0600Using timeit in an interact without string formattinghttp://ask.sagemath.org/question/9883/using-timeit-in-an-interact-without-string-formatting/I wanted the following to work, but it didn't. Anyone have an idea how to make it do so?
def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(n)')
Instead I had to do
def T(n):
top = ceil(math.sqrt(n))
print top
@interact
def _(n=6739815371):
T(n)
timeit('T(%s)'%n)
But I feel like the first thing did indeed work in the past (though I tried it with a Sage from 2010 to no avail).
I guess in some sense this is a silly question - `'T(n)'` is a string, pure and simple. At the same time, in the command line
sage: def T(n):
....: top = ceil(math.sqrt(n))
....: print top
....:
sage: n=6739815371
sage: timeit('T(n)')
625 loops, best of 3: 5.96 µs per loop
and `'T(n)'` is a string there too. So what is different about `interact` that this doesn't work (in Sage or the cell server)?kcrismanTue, 05 Mar 2013 14:21:49 -0600http://ask.sagemath.org/question/9883/timeit messing uphttp://ask.sagemath.org/question/9426/timeit-messing-up/On our Sage server, the timeit stopped working. It did not work in 5.2 and it still does not work in 5.3. I even copied examples directly from a [Sage reference page on timeit](http://www.sagemath.org/doc/reference/sage/misc/sage_timeit_class.html) and it still doesn't work. Any ideas?
timeit('2^10000')
or
timeit.eval('2^10000')
result in:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_31.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dGltZWl0KCcyXjEwMDAwJyk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpbMe7hW/___code___.py", line 2, in <module>
exec compile(u"timeit('2^10000')" + '\n', '', 'single')
File "", line 1, in <module>
TypeError: 'module' object is not callable
On the other hand, the following works fine, but it gives very low precision so it isn't really helpful.
%time
2^10000
Any ideas???
G-SageMon, 15 Oct 2012 05:23:57 -0500http://ask.sagemath.org/question/9426/Record output of timeit in variablehttp://ask.sagemath.org/question/8849/record-output-of-timeit-in-variable/How can I record in a variable the amount of time taken by a function? I read somewhere that timeit is the good way to time stuff, but it seems to print its output instead of returning it.
If possible, I would prefer not to redirect streams or redefine print, etc.
<br/>
EDIT: got it, leaving the question for reference.
timeit("blah",seconds=True)
returns a number.
This should probably get a mention in 'timeit?'.chaesloc2Tue, 03 Apr 2012 00:49:33 -0500http://ask.sagemath.org/question/8849/