[Python-checkins] python/dist/src/Lib timeit.py,1.5,1.6

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Thu, 06 Mar 2003 17:33:20 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv13342

Modified Files:
	timeit.py 
Log Message:
Broke down and made it work for Python 2.0 and up.  (Older versions
would have required refraining from using string methods -- too
painful.)

Changed the -s option so that multiple -s options are cumulative.


Index: timeit.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/timeit.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** timeit.py	6 Mar 2003 16:11:17 -0000	1.5
--- timeit.py	7 Mar 2003 01:33:18 -0000	1.6
***************
*** 1,3 ****
! """Framework for measuring execution time for small code snippets.
  
  This module avoids a number of common traps for measuring execution
--- 1,3 ----
! """Tool for measuring execution time of small code snippets.
  
  This module avoids a number of common traps for measuring execution
***************
*** 13,17 ****
    -n/--number N: how many times to execute 'statement' (default: see below)
    -r/--repeat N: how many times to repeat the timer (default 1)
!   -s/--setup S: statements executed once before 'statement' (default 'pass')
    -t/--time: use time.time() (default on Unix)
    -c/--clock: use time.clock() (default on Windows)
--- 13,17 ----
    -n/--number N: how many times to execute 'statement' (default: see below)
    -r/--repeat N: how many times to repeat the timer (default 1)
!   -s/--setup S: statement to be executed once initially (default 'pass')
    -t/--time: use time.time() (default on Unix)
    -c/--clock: use time.clock() (default on Windows)
***************
*** 21,25 ****
  A multi-line statement may be given by specifying each line as a
  separate argument; indented lines are possible by enclosing an
! argument in quotes and using leading spaces.
  
  If -n is not given, a suitable number of loops is calculated by trying
--- 21,26 ----
  A multi-line statement may be given by specifying each line as a
  separate argument; indented lines are possible by enclosing an
! argument in quotes and using leading spaces.  Multiple -s options are
! treated similarly.
  
  If -n is not given, a suitable number of loops is calculated by trying
***************
*** 38,62 ****
  Note: there is a certain baseline overhead associated with executing a
  pass statement.  The code here doesn't try to hide it, but you should
! be aware of it (especially when comparing different versions of
! Python).  The baseline overhead is measured by invoking the program
! without arguments.
! """
! 
! # To use this module with older versions of Python, the dependency on
! # the itertools module is easily removed; in the template, instead of
! # itertools.repeat(None, number), use [None]*number.  It's barely
! # slower.  Note: the baseline overhead, measured by the default
! # invocation, differs for older Python versions!  Also, to fairly
! # compare older Python versions to Python 2.3, you may want to use
! # python -O for the older versions to avoid timing SET_LINENO
! # instructions.
  
! # XXX Maybe for convenience of comparing with previous Python versions,
! # itertools.repeat() should not be used at all?
  
  import sys
  import math
  import time
! import itertools
  
  __all__ = ["Timer"]
--- 39,59 ----
  Note: there is a certain baseline overhead associated with executing a
  pass statement.  The code here doesn't try to hide it, but you should
! be aware of it.  The baseline overhead can be measured by invoking the
! program without arguments.
  
! The baseline overhead differs between Python versions!  Also, to
! fairly compare older Python versions to Python 2.3, you may want to
! use python -O for the older versions to avoid timing SET_LINENO
! instructions.
! """
  
  import sys
  import math
  import time
! try:
!     import itertools
! except ImportError:
!     # Must be an older Python version (see timeit() below)
!     itertools = None
  
  __all__ = ["Timer"]
***************
*** 76,82 ****
  # being indented 8 spaces.
  template = """
! def inner(number, timer):
      %(setup)s
-     seq = itertools.repeat(None, number)
      t0 = timer()
      for i in seq:
--- 73,78 ----
  # being indented 8 spaces.
  template = """
! def inner(seq, timer):
      %(setup)s
      t0 = timer()
      for i in seq:
***************
*** 127,131 ****
          the timer function to be used are passed to the constructor.
          """
!         return self.inner(number, self.timer)
  
      def repeat(self, repeat=default_repeat, number=default_number):
--- 123,131 ----
          the timer function to be used are passed to the constructor.
          """
!         if itertools:
!             seq = itertools.repeat(None, number)
!         else:
!             seq = [None] * number
!         return self.inner(seq, self.timer)
  
      def repeat(self, repeat=default_repeat, number=default_number):
***************
*** 178,182 ****
      stmt = "\n".join(args) or "pass"
      number = 0 # auto-determine
!     setup = "pass"
      repeat = 1
      for o, a in opts:
--- 178,182 ----
      stmt = "\n".join(args) or "pass"
      number = 0 # auto-determine
!     setup = []
      repeat = 1
      for o, a in opts:
***************
*** 184,188 ****
              number = int(a)
          if o in ("-s", "--setup"):
!             setup = a
          if o in ("-r", "--repeat"):
              repeat = int(a)
--- 184,188 ----
              number = int(a)
          if o in ("-s", "--setup"):
!             setup.append(a)
          if o in ("-r", "--repeat"):
              repeat = int(a)
***************
*** 196,199 ****
--- 196,200 ----
              print __doc__,
              return 0
+     setup = "\n".join(setup) or "pass"
      t = Timer(stmt, setup, timer)
      if number == 0: