My goal is to evaluate execution times between two different functions that output the same result. I am looking at np.linalg.det() and a function I made called mydet().
For each loop, I would like to generate a n x n matrix with n being in the range of (2,9). The 2 because any less wouldn't be a matrix, and 9 because any larger and the execution time would be significant.
I want the n value to correspond to the loop value i, so would n = i + 2? i starts at 0, and I need n to start at 2.
I want to measure the time it takes for each loop, and append it to an empty list called my_det_time and np_det_time, respectively.
Right now, my code only calculates the sum of the execution time. Whereas I would like to take each loop's time and append to the lists I specified.
What do I need to change in order to evaluate each loops' time and append that loops' time to a list, so I can see the progression of times as the matrix gets larger, and then plot the data to compare?
Thanks
import time
start_time = time.time()
from random import randint
my_det_time = []
np_det_time = []
for i in range(8):
n = i+2
s = 10
A = [[round(random.random()*s) for i in range(n)] for j in range(n)]
np.linalg.det(A)
print("%s seconds" % (time.time() - start_time))
You could have a list of times:
exe_times = []
for i in range(8):
start = time.time()
n = i+2
s = 10
A = [[round(random.random()*s) for i in range(n)] for j in range(n)]
np.linalg.det(A)
end = time.time()
exe_times.append(end-start)
But have in mind that this time is what is called wall clock time, i.e., the full time elapsed since the begin of the loop and the end of the loop. This time isn't the time that the CPU totally expends by just computing the instructions inside the loop. This could be a problem if, for instance, an CPU-bound program starts to run at same time that a low time duration loop of yours and stops before a high time duration loop. In a case like this, the time comparison would not be accurate.