What code does the Python timeit (...) method actually use in this bit of code?

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 task in a list of size n, but I cannot find a way to generate the list and then pass it as the variable timeit.timeit (...). Will the timer measure only the time that A0 takes to complete, or will the listing also be included in the measurement?

+4
source share
2 answers

It will measure the execution time of everything in the instruction (the first arg), so if you only need to measure the call to A0, then creating a list will distort the results.

Try creating a list in customization:

 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 be created only once, at the beginning of the timer and will not be included in the selection.

+3
source

List generation time will be added (regardless of how many quotes there will be when timeit is called).

0
source

Source: https://habr.com/ru/post/1439516/


All Articles