time - Python timeit and program output -
is there way use timeit function output both function result , time took process @ same time?
right using
timer = timer('func()', 'from __main__ import func') print timer.timeit(1) but outputs time , not program output, returns @ end. want output
funcoutputgoeshere 13.2897528935 on same line.
ideally, i'd able take average of program running n times , outputting program result , average time (a total of 1 output overall)
two options:
include 'print' in timed code. ugly, hey.
timer = timer('print func()', 'from __main__ import func') print timer.timeit(1)if run function once, dispense
timeitmodule altogether , time code directly using same method:import sys import time if sys.platform == "win32": # on windows, best timer time.clock() default_timer = time.clock else: # on other platforms best timer time.time() default_timer = time.time t0 = default_timer() output = func() t1 = default_timer() print output, t1 - t0
if want run code multiple times, , produce output, why not run code once outside timeit function? calling more once anyway:
timer = timer('func()', 'from __main__ import func') print timer.timeit(100), print func()
Comments
Post a Comment