mcdito13
mcdito13

Reputation: 321

How do I store the result of a function in a variable if I'm timing how long it takes using timeit?

I have to time how long it takes a function to run that returns a list. I want to store the time it takes it to run in a variable, and store the list the function returns. How do I go about storing both?

Example that returns the time but loses the result:

t1 = timeit.timeit(myFunction())

If myFunction returns a list, how do I store it and the time? Is this possible?

Upvotes: 1

Views: 483

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36662

You can do it the "old fashion way":

start = time.time()
var = myFunction()
end = time.time()
time_needed = end - start
print(var, time_needed)

Upvotes: 1

Related Questions