1111
1212Options:
1313 -n/--number N: how many times to execute 'statement' (default: see below)
14- -r/--repeat N: how many times to repeat the timer (default 1 )
14+ -r/--repeat N: how many times to repeat the timer (default 3 )
1515 -s/--setup S: statement to be executed once initially (default 'pass')
1616 -t/--time: use time.time() (default on Unix)
1717 -c/--clock: use time.clock() (default on Windows)
18+ -v/--verbose: print raw timing results; repeat for more digits precision
1819 -h/--help: print this usage message and exit
1920 statement: statement to be timed (default 'pass')
2021
3334functions measures wall clock time, not the CPU time. This means that
3435other processes running on the same computer may interfere with the
3536timing. The best thing to do when accurate timing is necessary is to
36- repeat the timing a few times and use the best time; the -r option is
37- good for this. On Unix, you can use clock() to measure CPU time.
37+ repeat the timing a few times and use the best time. The -r option is
38+ good for this; the default of 3 repetitions is probably enough in most
39+ cases. On Unix, you can use clock() to measure CPU time.
3840
3941Note: there is a certain baseline overhead associated with executing a
4042pass statement. The code here doesn't try to hide it, but you should
6062
6163dummy_src_name = "<timeit-src>"
6264default_number = 1000000
63- default_repeat = 10
65+ default_repeat = 3
6466
6567if sys .platform == "win32" :
6668 # On Windows, the best timer is time.clock()
@@ -159,7 +161,7 @@ def repeat(self, repeat=default_repeat, number=default_number):
159161
160162 This is a convenience function that calls the timer()
161163 repeatedly, returning a list of results. The first argument
162- specifies how many times to call timer(), defaulting to 10 ;
164+ specifies how many times to call timer(), defaulting to 3 ;
163165 the second argument specifies the timer argument, defaulting
164166 to one million.
165167
@@ -197,9 +199,9 @@ def main(args=None):
197199 args = sys .argv [1 :]
198200 import getopt
199201 try :
200- opts , args = getopt .getopt (args , "n:s:r:tch " ,
202+ opts , args = getopt .getopt (args , "n:s:r:tcvh " ,
201203 ["number=" , "setup=" , "repeat=" ,
202- "time" , "clock" , "help" ])
204+ "time" , "clock" , "verbose" , " help" ])
203205 except getopt .error , err :
204206 print err
205207 print "use -h/--help for command line help"
@@ -208,7 +210,9 @@ def main(args=None):
208210 stmt = "\n " .join (args ) or "pass"
209211 number = 0 # auto-determine
210212 setup = []
211- repeat = 1
213+ repeat = default_repeat
214+ verbose = 0
215+ precision = 3
212216 for o , a in opts :
213217 if o in ("-n" , "--number" ):
214218 number = int (a )
@@ -222,6 +226,10 @@ def main(args=None):
222226 timer = time .time
223227 if o in ("-c" , "--clock" ):
224228 timer = time .clock
229+ if o in ("-v" , "--verbose" ):
230+ if verbose :
231+ precision += 1
232+ verbose += 1
225233 if o in ("-h" , "--help" ):
226234 print __doc__ ,
227235 return 0
@@ -236,6 +244,8 @@ def main(args=None):
236244 except :
237245 t .print_exc ()
238246 return 1
247+ if verbose :
248+ print "%d loops -> %.*g secs" % (number , precision , x )
239249 if x >= 0.2 :
240250 break
241251 try :
@@ -244,12 +254,11 @@ def main(args=None):
244254 t .print_exc ()
245255 return 1
246256 best = min (r )
257+ if verbose :
258+ print "raw times:" , " " .join (["%.*g" % (precision , x ) for x in r ])
247259 print "%d loops," % number ,
248260 usec = best * 1e6 / number
249- if repeat > 1 :
250- print "best of %d: %.3f usec per loop" % (repeat , usec )
251- else :
252- print "time: %.3f usec per loop" % usec
261+ print "best of %d: %.*g usec per loop" % (repeat , precision , usec )
253262 return None
254263
255264if __name__ == "__main__" :
0 commit comments