4
timeit.timeit("A0([randint(1,256) * (-1) ** randint(1,2) for j in range("+str(n)+")])", setup="from HW2 import A0", number=1000000)

I want to measure the time that the A0 algorithm takes to complete its job on a list of size n, but I can't find a way to generate the list and then pass it in as a variable to the timeit.timeit(...) method. Will the timer only measure how long A0 takes to complete, or will the list generation also be included in the measurement?

2 Answers 2

3

It will measure the execution time of everything in the statement ( the first arg ), so if you only want the measurment of the call to A0, then the list creation will skew the results.

Try creating the list in the setup:

timeit.timeit("A0(aList)", setup="from HW2 import A0; aList = [randint(1,256) * (-1) ** randint(1,2) for j in range("+str(n)+")] ", number=1000000)

The list will only be created once, at the start of the timer, and not be included in the timing.

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

4 Comments

So is there a way to pass that list in as a variable?
I just showed ya. Do it in the setup. It will be available in the statement. Or make it a variable that comes with the import of A0
No problem. And if you check out ipython, it has a really great timeit integration that lets you run any statement on the fly with full access to the scope of variables and functions.
Dont forget to hit the checkmark if this solved your question!
0

The list generation time will be included (whatever is between quotes when calling timeit will be).

Comments

Your Answer

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