Question: Please help me with this part. Part 1 is done below with comments and code. Thanks Part 2: Runtime measurements with addition of profiler -
Please help me with this part. Part 1 is done below with comments and code. Thanks
Part 2: Runtime measurements with addition of profiler - Add the following: @profile right above the function definition. This will start profiling the code - Repeat the steps above, for the lower half of the range you used for Part 1 and observe what happens with the time needed for the computation, per element. You will also see some output from the profiler that shows the memory requirements of your code. Why do you think the runtimes behave like that now that you have enabled profiling? - Add a short description to your code file (at the top, in the documentation section) indicating what happens with the runtimes for Part 1. Why do you think the runtimes are now different?
Code is:
# DOCUMENTATION '''
Matrix with size 10 ELAPSED TIME : 0.000915 sec Number of elements in the resultant matrix : 100 Time per Element : 5.574226379394531e-06 Overall execution time : 1.2205727100372314 sec
Matrix with size 150 ELAPSED TIME : 10.017763 sec Number of elements in the resultant matrix : 22500 Time per Element : 0.00029305592642890084 Overall execution time : 14.663295269012451 sec
Matrix with size 400 ELAPSED TIME : 58.691846 sec Number of elements in the resultant matrix : 160000 Time per Element : 0.00023361390829086304 Overall execution time : 64.21486186981201 sec
'''
# Part 1: import time
program_start = time.time() # Initializing program timer
import random
#from memory_profiler import profile
time_lst = [] # Time per element list
#@profile def multiply(): # Matrix multiplication for i in range(size): for j in range(size): for k in range(size): start = time.time() # Time per element Start P[i][j] = P[i][j] + (A[i][k] * B[k][j]) end = time.time() # Time per element End time_lst.append((end - start))
size = int(input("Enter size : ")) r1 = size c1 = size r2 = size c2 = size
A = [[0 for i in range(c1)] for j in range(r1)] # Initialize matrix A
# input matrix A for i in range(r1): for j in range(c1): x = random.randint(1, 100) A[i][j] = x
B = [[0 for i in range(c2)] for j in range(r2)] # Initialize matrix B
# input matrix B for i in range(r2): for j in range(c2): x = random.randint(1, 100) B[i][j] = x
P = [[0 for i in range(c2)] for j in range(r1)] # Initialize product matrix
multiply_start = time.time() # INSERT CODE TO START TIMER multiply() multiply_end = time.time() # INSERT CODE TO STOP TIMER
print("ELAPSED TIME : %f sec " % (multiply_end - multiply_start)) # INSERT CODE TO COMPUTE ELAPSED TIME
# INSERT CODE TO COMPUTE ELAPSED TIME total_element = r1 * c2 # Resultant matrix will the [r1,c1]*[r2,c2] == [r1,c2] print("Number of elements in the resultant matrix :", total_element) # NUMBER OF ELEMENTS IN THE RESULT MATRIX
print("Time per Element :", sum(time_lst) / total_element) # TIME PER ELEMENT (IN ms) Calculated the average
print("Overall execution time : %s sec" % (time.time() - program_start)) # INSERT CODE TO PRINT OVERALL TIME (IN s)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
