Python timeit and program output
Asked Answered
S

2

15

Is there any way to use the timeit function to output both the function result and the time it took to process at the same time?

Right now I am using

timer = Timer('func()', 'from __main__ import func')
print timer.timeit(1)

But this just outputs the time and not the program output, which returns something at its end. I want it to output

FuncOutputGoesHere 13.2897528935

on the same line.

Ideally, I'd like to be able to take an average of the program by running it N times and then outputting the program result and its average time (a total of one output overall)

Selfimprovement answered 4/6, 2012 at 16:31 Comment(7)
I seem to see a lot of deviation between people recommending timeit and people recommending time.time(). Which is better?Selfimprovement
timeit uses time.time(), unless you are on windows, where it'll use time.clock (more accurate).Dagoba
So timeit will provide me with the most accuracy?Selfimprovement
My answer gives you the same code as what timeit uses. It'll be just as accurate.Dagoba
Updated my answer; if you are running it more than once anyway, why not run it once more to capture the return value?Dagoba
@MartijnPieters Hmmmmm, hard to argue with that. Would I just divide it like so: timer.timeit(N)/N? Can I refer to functions dynamically? For instance, I can time a function dynamically by replacing the arguments in the string. Can I do the same for the actual function call?Selfimprovement
Correct; the examples in the timeit documentation then multiply that value by 1,000,000 again to get a microsecond value instead of seconds, but they basically do timer.timeit(N)/N, yes.Dagoba
D
19

Two options:

  1. Include 'print' in your timed code. Ugly, but hey.

    timer = Timer('print func()', 'from __main__ import func')
    print timer.timeit(1)
    
  2. If all you do is run your function once, then dispense with the timeit module altogether and time the code directly using the same method:

    import sys
    import time
    
    if sys.platform == "win32":
        # On Windows, the best timer is time.clock()
        default_timer = time.clock
    else:
        # On most other platforms the best timer is time.time()
        default_timer = time.time
    
    t0 = default_timer()
    output = func()
    t1 = default_timer()
    print output, t1 - t0
    

If you want to run the code multiple times, and produce the output, why not run the code once outside the timeit function? You are calling it more than once then already anyway:

    timer = Timer('func()', 'from __main__ import func')
    print timer.timeit(100),
    print func()
Dagoba answered 4/6, 2012 at 16:42 Comment(0)
D
0

The timeit module executes the statement passed. You could just print the result of the function:

timer = Timer('print func()', 'from __main__ import func')
print timer.timeit(1)
Devaughn answered 4/6, 2012 at 16:43 Comment(4)
This would print it a million times, or however often the statement is executed, and also count the I/O overhead into the timings.Allergic
@delnan: he runs timer.timeit(1)... Make of that what you will. :-PDagoba
@MartijnPieters I don't know how to run it multiple times without it outputting multiple times; also I think I'd need to divide the result by the number of runtimes because it seems to be aggregate. It'd be ideal to run it a few times to get an average and output once.Selfimprovement
Yes, it aggregates total runtime. See the timeit documentation.Dagoba

© 2022 - 2024 — McMap. All rights reserved.