-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnumpy_example.py
More file actions
57 lines (45 loc) · 1.58 KB
/
numpy_example.py
File metadata and controls
57 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
from time import time
def matrix_mul(size, n=100):
# reference: https://markus-beuckelmann.de/blog/boosting-numpy-blas.html
np.random.seed(112)
a, b = np.random.random((size, size)), np.random.random((size, size))
t = time()
for _ in range(n):
np.dot(a, b)
delta = time() - t
print('Dotted two matrices of size %dx%d in %0.4f ms.' % (size, size, delta / n * 1000))
def vector_mul(size, n=100):
# reference: https://markus-beuckelmann.de/blog/boosting-numpy-blas.html
np.random.seed(112)
a, b = np.random.random((size, size)), np.random.random((size, size))
t = time()
for _ in range(n):
np.dot(a, b)
delta = time() - t
print('Dotted two vector of length %d in %0.4f ms.' % (size, delta / n * 1000))
def svd_decomposition(size, n=10):
np.random.seed(112)
a = np.random.random((size, size))
t = time()
for _ in range(n):
np.linalg.svd(a, full_matrices=False)
delta = time() - t
print('SVD decomposition of size %dx%d in %0.4f ms.' % (size, size, delta / n * 1000))
def eigen_decomposition(size, n=10):
np.random.seed(112)
a = np.random.random((size, size))
t = time()
for _ in range(n):
np.linalg.eig(a)
delta = time() - t
print('Eigen decomposition of size %dx%d in %0.4f ms.' % (size, size, delta / n * 1000))
def main():
print('Numpy config')
print(np.__config__.show())
vector_mul(size=8192, n=50)
matrix_mul(size=8192, n=50)
svd_decomposition(2048, n=50)
eigen_decomposition(2048, n=50)
if __name__ == '__main__':
main()