### Numpy exercise

Posted:

**Thu Feb 18, 2021 12:49 am**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!

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
L1=[]
L2=[]
for e, f in zip(a, b):
L1.append(e)
L2.append(f)
def slow_matrix_multiplication(L1, L2):
result = 0
for i in range(len(L1)):
result += L1[i]*L2[i]
return result
t0 = datetime.now()
for t in range(T):
slow_matrix_multiplication(L1, L2)
dt1 = datetime.now() - t0
t0 = datetime.now()
for t in range(T):
a.dot(b)
dt2 = datetime.now() - t0
print("dt1 / dt2: ", dt1.total_seconds() / dt2.total_seconds())
```