0

In Matlab there is

"timeit(F), which measures the typical time (in seconds) required to run the function specified by the function handle F that takes no input argument."

This method returns the median of (I think 13) runs of the function.

Having looked at the methods time and timeit in Python, I can't quite find anything that will let me call from an IPython console, time my script (not function) a number of times, and return either an average or median.

Is there an easy way to do this? or at least time 1 execution, whereby I can make my own loop and average? Thanks

1 Answer 1

1

You may want to look at this link and consider the %timeit magic from IPython

link


Example:

Say you define a function you want to test:

def logtest1(N):
tr=0.
for i in xrange(N):
    T= 40. + 10.*random()
    tr = tr + -log(random())/T

from timeit import Timer, timeit, repeat
runningtime = repeat("logtest1(int(10e5))", setup="from __main__ import logtest1", repeat=5, number=1)
print (runningtime)

That will run my function logtest1(int(10e5)) 1 time and store the time in the list runningtime then it will repeat the same thing 5 times and store the results in the same list. You can then take the average of the median of that list.

Sign up to request clarification or add additional context in comments.

3 Comments

Having looked at it, there is no specification about running scripts from this? Or would I be implementing this within the script?
Yes, you should do from within the script. Or at least load the script into IPython, make it a function and run it like that. Another alternative would be chaining the Ipython magics %timeit %run test.py (you can customize the timeit magic options if needed)
I used %timeit -n10 %run test which runs for 10 loops and 3(default, but changed by adding -r after -n) times, and takes the best of the 3 runs of 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.