CoCalc Public Filessage_worksheets / ADS_RSA.sagewsOpen with one click!
Author: Ken Levasseur
Description: Worksheets related to Applied Discrete Structures
def random_n_bit_prime(n): return random_prime(2^n-1,lbound=2^(n-1))
def factor_time(bits): return timeit('factor(random_n_bit_prime('+bits.str()+')*random_n_bit_prime('+bits.str()+'))',seconds=True)
for k in srange(10,90,10): factor_time(k)
8.05362701416e-05 0.000219452667236 0.00155693435669 0.00596276092529 0.0131066799164 0.0466529846191 0.213326597214 0.866991996765
map(lambda k:[k,factor_time(k)],srange(5,30,5))
4.76112365723e-05 8.1022644043e-05 0.00013451385498 0.000207384109497 0.000469622421265 [[5, None], [10, None], [15, None], [20, None], [25, None]]
g=lambda k:[k,factor_time(k)]
0.00633575248718 [40, None]
File: /cocalc/lib/python2.7/site-packages/smc_sagews/ Signature : timeit(*args, **kwds) Docstring : Time execution of a command or block of commands. This command has been enhanced for Salvus so you may use it as a block decorator as well, e.g., %timeit 2+3 and %timeit(number=10, preparse=False) 2^3 %timeit(number=10, seconds=True) 2^3 and %timeit(preparse=False) [rest of the cell] Here is the original docstring for timeit: nodetex Accurately measure the wall time required to execute "stmt". INPUT: * "stmt" -- a text string. * "globals_dict" -- a dictionary or "None" (default). Evaluate "stmt" in the context of the globals dictionary. If not set, the current "globals()" dictionary is used. * "preparse" -- (default: use globals preparser default) if "True" preparse "stmt" using the Sage preparser. * "number" -- integer, (optional, default: 0), number of loops. * "repeat" -- integer, (optional, default: 3), number of repetition. * "precision" -- integer, (optional, default: 3), precision of output time. * "seconds" -- boolean (default: "False"). Whether to just return time in seconds. OUTPUT: An instance of "SageTimeitResult" unless the optional parameter "seconds=True" is passed. In that case, the elapsed time in seconds is returned as a floating-point number. EXAMPLES: sage: from sage.misc.sage_timeit import sage_timeit sage: sage_timeit('3^100000', globals(), preparse=True, number=50) # random output '50 loops, best of 3: 1.97 ms per loop' sage: sage_timeit('3^100000', globals(), preparse=False, number=50) # random output '50 loops, best of 3: 67.1 ns per loop' sage: a = 10 sage: sage_timeit('a^2', globals(), number=50) # random output '50 loops, best of 3: 4.26 us per loop' If you only want to see the timing and not have access to additional information, just use the "timeit" object: sage: timeit('10^2', number=50) 50 loops, best of 3: ... per loop Using sage_timeit gives you more information though: sage: s = sage_timeit('10^2', globals(), repeat=1000) sage: len(s.series) 1000 sage: mean(s.series) # random output 3.1298141479492283e-07 sage: min(s.series) # random output 2.9258728027343752e-07 sage: t = stats.TimeSeries(s.series) sage: t.scale(10^6).plot_histogram(bins=20,figsize=[12,6], ymax=2) Graphics object consisting of 20 graphics primitives The input expression can contain newlines (but doctests cannot, so we use "os.linesep" here): sage: from sage.misc.sage_timeit import sage_timeit sage: from os import linesep as CR sage: # sage_timeit(r'a = 2nb=131nfactor(a^b-1)') sage: sage_timeit('a = 2' + CR + 'b=131' + CR + 'factor(a^b-1)', ....: globals(), number=10) 10 loops, best of 3: ... per loop Test to make sure that "timeit" behaves well with output: sage: timeit("print 'Hi'", number=50) 50 loops, best of 3: ... per loop If you want a machine-readable output, use the "seconds=True" option: sage: timeit("print 'Hi'", seconds=True) # random output 1.42555236816e-06 sage: t = timeit("print 'Hi'", seconds=True) sage: t #r random output 3.6010742187499999e-07