@@ -264,14 +264,15 @@ def main(args=None, *, _wrap_timer=None):
264264 print (err )
265265 print ("use -h/--help for command line help" )
266266 return 2
267+
267268 timer = default_timer
268269 stmt = "\n " .join (args ) or "pass"
269270 number = 0 # auto-determine
270271 setup = []
271272 repeat = default_repeat
272273 verbose = 0
273274 time_unit = None
274- units = {"usec" : 1 , "msec" : 1e3 , "sec" : 1e6 }
275+ units = {"usec" : 1e-6 , "msec" : 1e-3 , "sec" : 1.0 }
275276 precision = 3
276277 for o , a in opts :
277278 if o in ("-n" , "--number" ):
@@ -299,13 +300,15 @@ def main(args=None, *, _wrap_timer=None):
299300 print (__doc__ , end = ' ' )
300301 return 0
301302 setup = "\n " .join (setup ) or "pass"
303+
302304 # Include the current directory, so that local imports work (sys.path
303305 # contains the directory of this script, rather than the current
304306 # directory)
305307 import os
306308 sys .path .insert (0 , os .curdir )
307309 if _wrap_timer is not None :
308310 timer = _wrap_timer (timer )
311+
309312 t = Timer (stmt , setup , timer )
310313 if number == 0 :
311314 # determine number so that 0.2 <= total time < 2.0
@@ -321,37 +324,47 @@ def callback(number, time_taken):
321324 except :
322325 t .print_exc ()
323326 return 1
327+
324328 try :
325- r = t .repeat (repeat , number )
329+ raw_timings = t .repeat (repeat , number )
326330 except :
327331 t .print_exc ()
328332 return 1
329- best = min (r )
333+
334+ def format_time (dt ):
335+ unit = time_unit
336+
337+ if unit is not None :
338+ scale = units [unit ]
339+ else :
340+ scales = [(scale , unit ) for unit , scale in units .items ()]
341+ scales .sort (reverse = True )
342+ for scale , unit in scales :
343+ if dt >= scale :
344+ break
345+
346+ return "%.*g %s" % (precision , dt / scale , unit )
347+
330348 if verbose :
331- print ("raw times:" , " " .join (["%.*g" % (precision , x ) for x in r ]))
332- print ("%d loop%s," % (number , 's' if number != 1 else '' ), end = ' ' )
333- usec = best * 1e6 / number
334- if time_unit is not None :
335- scale = units [time_unit ]
336- else :
337- scales = [(scale , unit ) for unit , scale in units .items ()]
338- scales .sort (reverse = True )
339- for scale , time_unit in scales :
340- if usec >= scale :
341- break
342- print ("best of %d: %.*g %s per loop" % (repeat , precision ,
343- usec / scale , time_unit ))
344- best = min (r )
345- usec = best * 1e6 / number
346- worst = max (r )
349+ print ("raw times: %s" % ", " .join (map (format_time , raw_timings )))
350+
351+ timings = [dt / number for dt in raw_timings ]
352+
353+ best = min (timings )
354+ print ("%d loop%s, best of %d: %s per loop"
355+ % (number , 's' if number != 1 else '' ,
356+ repeat , format_time (best )))
357+
358+ best = min (timings )
359+ worst = max (timings )
347360 if worst >= best * 4 :
348- usec = worst * 1e6 / number
349361 import warnings
350- warnings .warn_explicit (
351- "The test results are likely unreliable. The worst\n "
352- "time (%.*g %s) was more than four times slower than the best time." %
353- (precision , usec / scale , time_unit ),
354- UserWarning , '' , 0 )
362+ warnings .warn_explicit ("The test results are likely unreliable. "
363+ "The worst time (%s) was more than four times "
364+ "slower than the best time (%s)."
365+ % (precision ,
366+ format_time (worst ), format_time (best )),
367+ UserWarning , '' , 0 )
355368 return None
356369
357370if __name__ == "__main__" :
0 commit comments