Search code examples
pythonpython-3.xbenchmarkingmicrobenchmarktimeit

How to use python timeit to get median runtime


I want to benchmark a bit of python code (not the language I am used to, but I have to do some comparisons in python). I have understood that timeit is a good tool for this, and I have a code like this:

n = 10
duration = timeit.Timer(my_func).timeit(number=n)
duration/n

to measure the mean runtime of the function. Now, I want to instead have the median time (the reason is that I want to make a comparison to something I get in median time, and it would be good to use the same measure in all cases). Now, timeit only seems to return the full runtime, and not the time of each individual run, so I am not sure how to find the median runtime. What is the best way to get this?


Solution

  • You can use the repeat method instead, which gives you the individual times as a list:

    import timeit
    from statistics import median
    
    def my_func():
        for _ in range(1000000):
            pass
    
    n = 10
    durations = timeit.Timer(my_func).repeat(repeat=n, number=1)
    print(median(durations))
    

    Try it online!