diff -r dc1eeca2dfb0 Lib/timeit.py --- a/Lib/timeit.py Tue Nov 26 14:58:10 2013 -0600 +++ b/Lib/timeit.py Tue Nov 26 17:26:30 2013 -0500 @@ -19,6 +19,7 @@ -t/--time: use time.time() (deprecated) -c/--clock: use time.clock() (deprecated) -v/--verbose: print raw timing results; repeat for more digits precision + -u/--unit: set the output time unit (usec, msec, or sec) -h/--help: print this usage message and exit --: separate options from statement, use when statement starts with - statement: statement to be timed (default 'pass') @@ -238,10 +239,10 @@ args = sys.argv[1:] import getopt try: - opts, args = getopt.getopt(args, "n:s:r:tcpvh", + opts, args = getopt.getopt(args, "n:u:s:r:tcpvh", ["number=", "setup=", "repeat=", "time", "clock", "process", - "verbose", "help"]) + "verbose", "unit=", "help"]) except getopt.error as err: print(err) print("use -h/--help for command line help") @@ -252,12 +253,19 @@ setup = [] repeat = default_repeat verbose = 0 + time_unit = "default" precision = 3 for o, a in opts: if o in ("-n", "--number"): number = int(a) if o in ("-s", "--setup"): setup.append(a) + if o in ("-u", "--unit"): + if a and a in ("usec", "msec", "sec"): + time_unit = str(a) + else: + print("Unrecognized unit. Please select usec, msec, or sec.") + return if o in ("-r", "--repeat"): repeat = int(a) if repeat <= 0: @@ -307,15 +315,25 @@ print("raw times:", " ".join(["%.*g" % (precision, x) for x in r])) print("%d loops," % number, end=' ') usec = best * 1e6 / number - if usec < 1000: - print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) - else: - msec = usec / 1000 - if msec < 1000: + if time_unit is not 'default': + if time_unit == 'usec': + print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) + elif time_unit == 'msec': + msec = usec / 1000 print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) else: - sec = msec / 1000 + sec = usec / 1000000 print("best of %d: %.*g sec per loop" % (repeat, precision, sec)) + else: + if usec < 1000: + print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) + else: + msec = usec / 1000 + if msec < 1000: + print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) + else: + sec = msec / 1000 + print("best of %d: %.*g sec per loop" % (repeat, precision, sec)) return None if __name__ == "__main__":