[Python-checkins] python/dist/src/Lib timeit.py,1.7,1.8

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Sat, 15 Mar 2003 04:25:02 -0800


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

Modified Files:
	timeit.py 
Log Message:
Change the default number of repetitions to 3, both in the Timer class
(from 10) and in main() (from 1).

Add a -v option that shows the raw times.  Repeating it cranks up the
display precision.

Always use the "best of N" form of output.


Index: timeit.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/timeit.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** timeit.py	14 Mar 2003 17:21:00 -0000	1.7
--- timeit.py	15 Mar 2003 12:25:00 -0000	1.8
***************
*** 12,19 ****
  Options:
    -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)
    -h/--help: print this usage message and exit
    statement: statement to be timed (default 'pass')
--- 12,20 ----
  Options:
    -n/--number N: how many times to execute 'statement' (default: see below)
!   -r/--repeat N: how many times to repeat the timer (default 3)
    -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)
+   -v/--verbose: print raw timing results; repeat for more digits precision
    -h/--help: print this usage message and exit
    statement: statement to be timed (default 'pass')
***************
*** 34,39 ****
  other processes running on the same computer may interfere with the
  timing.  The best thing to do when accurate timing is necessary is to
! repeat the timing a few times and use the best time; the -r option is
! good for this.  On Unix, you can use clock() to measure CPU time.
  
  Note: there is a certain baseline overhead associated with executing a
--- 35,41 ----
  other processes running on the same computer may interfere with the
  timing.  The best thing to do when accurate timing is necessary is to
! repeat the timing a few times and use the best time.  The -r option is
! good for this; the default of 3 repetitions is probably enough in most
! cases.  On Unix, you can use clock() to measure CPU time.
  
  Note: there is a certain baseline overhead associated with executing a
***************
*** 61,65 ****
  dummy_src_name = "<timeit-src>"
  default_number = 1000000
! default_repeat = 10
  
  if sys.platform == "win32":
--- 63,67 ----
  dummy_src_name = "<timeit-src>"
  default_number = 1000000
! default_repeat = 3
  
  if sys.platform == "win32":
***************
*** 160,164 ****
          This is a convenience function that calls the timer()
          repeatedly, returning a list of results.  The first argument
!         specifies how many times to call timer(), defaulting to 10;
          the second argument specifies the timer argument, defaulting
          to one million.
--- 162,166 ----
          This is a convenience function that calls the timer()
          repeatedly, returning a list of results.  The first argument
!         specifies how many times to call timer(), defaulting to 3;
          the second argument specifies the timer argument, defaulting
          to one million.
***************
*** 198,204 ****
      import getopt
      try:
!         opts, args = getopt.getopt(args, "n:s:r:tch",
                                     ["number=", "setup=", "repeat=",
!                                     "time", "clock", "help"])
      except getopt.error, err:
          print err
--- 200,206 ----
      import getopt
      try:
!         opts, args = getopt.getopt(args, "n:s:r:tcvh",
                                     ["number=", "setup=", "repeat=",
!                                     "time", "clock", "verbose", "help"])
      except getopt.error, err:
          print err
***************
*** 209,213 ****
      number = 0 # auto-determine
      setup = []
!     repeat = 1
      for o, a in opts:
          if o in ("-n", "--number"):
--- 211,217 ----
      number = 0 # auto-determine
      setup = []
!     repeat = default_repeat
!     verbose = 0
!     precision = 3
      for o, a in opts:
          if o in ("-n", "--number"):
***************
*** 223,226 ****
--- 227,234 ----
          if o in ("-c", "--clock"):
              timer = time.clock
+         if o in ("-v", "--verbose"):
+             if verbose:
+                 precision += 1
+             verbose += 1
          if o in ("-h", "--help"):
              print __doc__,
***************
*** 237,240 ****
--- 245,250 ----
                  t.print_exc()
                  return 1
+             if verbose:
+                 print "%d loops -> %.*g secs" % (number, precision, x)
              if x >= 0.2:
                  break
***************
*** 245,254 ****
          return 1
      best = min(r)
      print "%d loops," % number,
      usec = best * 1e6 / number
!     if repeat > 1:
!         print "best of %d: %.3f usec per loop" % (repeat, usec)
!     else:
!         print "time: %.3f usec per loop" % usec
      return None
  
--- 255,263 ----
          return 1
      best = min(r)
+     if verbose:
+         print "raw times:", " ".join(["%.*g" % (precision, x) for x in r])
      print "%d loops," % number,
      usec = best * 1e6 / number
!     print "best of %d: %.*g usec per loop" % (repeat, precision, usec)
      return None