The timeit command - Utilities

Skip to content
Back to top
View this page
Edit this page

# The `timeit` command

This uses the function `sage_timeit()`.

class sage.misc.sage_timeit_class.SageTimeit

  Bases: `object`

  Time execution of a command or block of commands.

  Displays the best WALL TIME for execution of the given code. This is based on the Python `timeit` module, which avoids a number of common traps for measuring execution times. It is also based on IPython’s `%timeit` command.

  TYPICAL INPUT FORMAT:

      timeit(statement, preparse=None, number=0, repeat=3, precision=3)

  EXAMPLES:

  Sage

      sage: timeit('2^10000')
      625 loops, best of 3: ... per loop

  Python

      >>> from sage.all import *
      >>> timeit('2^10000')
      625 loops, best of 3: ... per loop

  We illustrate some options:

  Sage

      sage: timeit('2+2',precision=2,number=20,repeat=5)
      20 loops, best of 5: ... per loop

  [Python]

      >>> from sage.all import *
      >>> timeit('2+2',precision=Integer(2),number=Integer(20),repeat=Integer(5))
      20 loops, best of 5: ... per loop

  The preparser is on by default (if it is on), but the preparse option allows us to override it:

  Sage

      sage: timeit('2^10000', preparse=False, number=50)
      50 loops, best of 3: ... per loop

  Python

      >>> from sage.all import *
      >>> timeit('2^10000', preparse=False, number=Integer(50))
      50 loops, best of 3: ... per loop

  The input can contain newlines:

  Sage

      sage: timeit("a = 2\nb=131\nfactor(a^b-1)", number=25)                          # needs sage.libs.pari
      25 loops, best of 3: ... per loop

  [Python]

      >>> from sage.all import *
      >>> timeit("a = 2\nb=131\nfactor(a^b-1)", number=Integer(25))                          # needs sage.libs.pari
      25 loops, best of 3: ... per loop

  See also

  `runsnake()`

  eval(code, globs=None, locals=None, **kwds)

    This eval function is called when doing %timeit in the notebook.

    INPUT:

    - `code` – string of code to evaluate; may contain newlines

    - `globs` – global variables; if not given, uses module scope globals

    - `locals` – ignored completely

    - `kwds` – passed onto sage_timeit. Common options are `preparse`, `number`, `repeat`, `precision`. See `sage_timeit()` for details.

    OUTPUT: string; timing information

    EXAMPLES:

    Sage

        sage: timeit.eval("2+2")                     # random output
        '625 loops, best of 3: 1.47 us per loop'

    Python

        >>> from sage.all import *
        >>> timeit.eval("2+2")                     # random output
        '625 loops, best of 3: 1.47 us per loop'

    We emphasize that timeit times WALL TIME. This is good in the context of Sage where commands often call out to other subprocesses that do not appear in CPU time.

    Sage

        sage: timeit('sleep(float(0.5))', number=3)  # long time (5s on sage.math, 2012)
        3 loops, best of 3: ... ms per loop

    [Python]

        >>> from sage.all import *
        >>> timeit('sleep(float(0.5))', number=Integer(3))  # long time (5s on sage.math, 2012)
        3 loops, best of 3: ... ms per loop