Page 1 of 1

Numpy exercise

Posted: Thu Feb 18, 2021 12:49 am
by klayperson
I didn't see a post about this exercise and I wanted to compare with others to see if I made the right approach.
I appended the values from the randomly generated array in order to make my lists.
From my result, using the array dot function is about 34-39 times faster than doing matrix multiplication using lists.
This ratio increases linearly with the increase in input size (i.e. 2x inputs = 2x dt1/dt2)

Let me know what y'all got. And any feedback on my code would be great!

Code: Select all

from datetime import datetime
a = np.random.randn(100)
b = np.random.randn(100)
T = 100000


for e, f in zip(a, b):

def slow_matrix_multiplication(L1, L2):
  result = 0
  for i in range(len(L1)):
    result += L1[i]*L2[i]
  return result

t0 =
for t in range(T):
  slow_matrix_multiplication(L1, L2)
dt1 = - t0

t0 =
for t in range(T):
dt2 = - t0

print("dt1 / dt2: ", dt1.total_seconds() / dt2.total_seconds())