Back to index

python3.2  3.2.2
Classes | Functions | Variables
timeit Namespace Reference

Classes

class  Timer

Functions

def reindent
def _template_func
def timeit
def repeat
def main

Variables

 itertools = None
list __all__ = ["Timer"]
string dummy_src_name = "<timeit-src>"
int default_number = 1000000
int default_repeat = 3
 default_timer = time.clock
string template

Function Documentation

def timeit._template_func (   setup,
  func 
) [private]
Create a timer function. Used if the "statement" is a callable.

Definition at line 94 of file timeit.py.

00094 
00095 def _template_func(setup, func):
00096     """Create a timer function. Used if the "statement" is a callable."""
00097     def inner(_it, _timer, _func=func):
00098         setup()
00099         _t0 = _timer()
00100         for _i in _it:
00101             _func()
00102         _t1 = _timer()
00103         return _t1 - _t0
00104     return inner

Here is the call graph for this function:

def timeit.main (   args = None,
  _wrap_timer = None 
)
Main program, used when run as a script.

The optional 'args' argument specifies the command line to be parsed,
defaulting to sys.argv[1:].

The return value is an exit code to be passed to sys.exit(); it
may be None to indicate success.

When an exception happens during timing, a traceback is printed to
stderr and the return value is 1.  Exceptions at other times
(including the template compilation) are not caught.

'_wrap_timer' is an internal interface used for unit testing.  If it
is not None, it must be a callable that accepts a timer function
and returns another timer function (used for unit testing).

Definition at line 237 of file timeit.py.

00237 
00238 def main(args=None, *, _wrap_timer=None):
00239     """Main program, used when run as a script.
00240 
00241     The optional 'args' argument specifies the command line to be parsed,
00242     defaulting to sys.argv[1:].
00243 
00244     The return value is an exit code to be passed to sys.exit(); it
00245     may be None to indicate success.
00246 
00247     When an exception happens during timing, a traceback is printed to
00248     stderr and the return value is 1.  Exceptions at other times
00249     (including the template compilation) are not caught.
00250 
00251     '_wrap_timer' is an internal interface used for unit testing.  If it
00252     is not None, it must be a callable that accepts a timer function
00253     and returns another timer function (used for unit testing).
00254     """
00255     if args is None:
00256         args = sys.argv[1:]
00257     import getopt
00258     try:
00259         opts, args = getopt.getopt(args, "n:s:r:tcvh",
00260                                    ["number=", "setup=", "repeat=",
00261                                     "time", "clock", "verbose", "help"])
00262     except getopt.error as err:
00263         print(err)
00264         print("use -h/--help for command line help")
00265         return 2
00266     timer = default_timer
00267     stmt = "\n".join(args) or "pass"
00268     number = 0 # auto-determine
00269     setup = []
00270     repeat = default_repeat
00271     verbose = 0
00272     precision = 3
00273     for o, a in opts:
00274         if o in ("-n", "--number"):
00275             number = int(a)
00276         if o in ("-s", "--setup"):
00277             setup.append(a)
00278         if o in ("-r", "--repeat"):
00279             repeat = int(a)
00280             if repeat <= 0:
00281                 repeat = 1
00282         if o in ("-t", "--time"):
00283             timer = time.time
00284         if o in ("-c", "--clock"):
00285             timer = time.clock
00286         if o in ("-v", "--verbose"):
00287             if verbose:
00288                 precision += 1
00289             verbose += 1
00290         if o in ("-h", "--help"):
00291             print(__doc__, end=' ')
00292             return 0
00293     setup = "\n".join(setup) or "pass"
00294     # Include the current directory, so that local imports work (sys.path
00295     # contains the directory of this script, rather than the current
00296     # directory)
00297     import os
00298     sys.path.insert(0, os.curdir)
00299     if _wrap_timer is not None:
00300         timer = _wrap_timer(timer)
00301     t = Timer(stmt, setup, timer)
00302     if number == 0:
00303         # determine number so that 0.2 <= total time < 2.0
00304         for i in range(1, 10):
00305             number = 10**i
00306             try:
00307                 x = t.timeit(number)
00308             except:
00309                 t.print_exc()
00310                 return 1
00311             if verbose:
00312                 print("%d loops -> %.*g secs" % (number, precision, x))
00313             if x >= 0.2:
00314                 break
00315     try:
00316         r = t.repeat(repeat, number)
00317     except:
00318         t.print_exc()
00319         return 1
00320     best = min(r)
00321     if verbose:
00322         print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
00323     print("%d loops," % number, end=' ')
00324     usec = best * 1e6 / number
00325     if usec < 1000:
00326         print("best of %d: %.*g usec per loop" % (repeat, precision, usec))
00327     else:
00328         msec = usec / 1000
00329         if msec < 1000:
00330             print("best of %d: %.*g msec per loop" % (repeat, precision, msec))
00331         else:
00332             sec = msec / 1000
00333             print("best of %d: %.*g sec per loop" % (repeat, precision, sec))
00334     return None

Here is the call graph for this function:

Here is the caller graph for this function:

def timeit.reindent (   src,
  indent 
)
Helper to reindent a multi-line statement.

Definition at line 90 of file timeit.py.

00090 
00091 def reindent(src, indent):
00092     """Helper to reindent a multi-line statement."""
00093     return src.replace("\n", "\n" + " "*indent)

Here is the caller graph for this function:

def timeit.repeat (   stmt = "pass",
  setup = "pass",
  timer = default_timer,
  repeat = default_repeat,
  number = default_number 
)
Convenience function to create Timer object and call repeat method.

Definition at line 233 of file timeit.py.

00233 
00234            repeat=default_repeat, number=default_number):
00235     """Convenience function to create Timer object and call repeat method."""
00236     return Timer(stmt, setup, timer).repeat(repeat, number)

Here is the caller graph for this function:

def timeit.timeit (   stmt = "pass",
  setup = "pass",
  timer = default_timer,
  number = default_number 
)
Convenience function to create Timer object and call timeit method.

Definition at line 228 of file timeit.py.

00228 
00229            number=default_number):
00230     """Convenience function to create Timer object and call timeit method."""
00231     return Timer(stmt, setup, timer).timeit(number)

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

list timeit.__all__ = ["Timer"]

Definition at line 64 of file timeit.py.

Definition at line 67 of file timeit.py.

Definition at line 68 of file timeit.py.

timeit.default_timer = time.clock

Definition at line 72 of file timeit.py.

Definition at line 66 of file timeit.py.

Definition at line 62 of file timeit.py.

Initial value:
00001 """
00002 def inner(_it, _timer):
00003     %(setup)s
00004     _t0 = _timer()
00005     for _i in _it:
00006         %(stmt)s
00007     _t1 = _timer()
00008     return _t1 - _t0
00009 """

Definition at line 80 of file timeit.py.