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:

  1. include 'print' in timed code. ugly, hey.

    timer = timer('print func()', 'from __main__ import func') print timer.timeit(1) 
  2. if run function once, dispense timeit module 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

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -