diff -r ebe28dba4a78 Lib/test/test_timeit.py --- a/Lib/test/test_timeit.py Mon Dec 09 13:19:23 2013 +0100 +++ b/Lib/test/test_timeit.py Mon Dec 09 09:35:19 2013 -0500 @@ -288,6 +288,24 @@ 10000 loops, best of 3: 50 usec per loop """)) + def test_main_with_time_unit(self): + unit_sec = self.run_main(seconds_per_increment=0.002, + switches=['-u', 'sec']) + self.assertEqual(unit_sec, + "1000 loops, best of 3: 0.002 sec per loop\n") + unit_msec = self.run_main(seconds_per_increment=0.002, + switches=['-u', 'msec']) + self.assertEqual(unit_msec, + "1000 loops, best of 3: 2 msec per loop\n") + unit_usec = self.run_main(seconds_per_increment=0.002, + switches=['-u', 'usec']) + self.assertEqual(unit_usec, + "1000 loops, best of 3: 2e+03 usec per loop\n") + # Test invalid unit input + with self.assertRaises(ValueError): + self.run_main(seconds_per_increment=0.02, + switches=["-u", "parsec"]) + def test_main_exception(self): with captured_stderr() as error_stringio: s = self.run_main(switches=['1/0']) diff -r ebe28dba4a78 Lib/timeit.py --- a/Lib/timeit.py Mon Dec 09 13:19:23 2013 +0100 +++ b/Lib/timeit.py Mon Dec 09 09:35:19 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 in ("usec", "msec", "sec"): + time_unit = a + else: + raise ValueError("Unrecognized unit. Please select usec, msec, or sec.") + return 2 if o in ("-r", "--repeat"): repeat = int(a) if repeat <= 0: @@ -307,15 +315,20 @@ 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)) + if time_unit is not 'default': + unit_dict = {"usec": usec, "msec": usec/1000, "sec": usec/1000000} + print("best of %d: %.*g %s per loop" % (repeat, precision, + unit_dict[time_unit], time_unit)) else: - msec = usec / 1000 - if msec < 1000: - print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) + if usec < 1000: + print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) else: - sec = msec / 1000 - print("best of %d: %.*g sec per loop" % (repeat, precision, sec)) + 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__":