Repository navigation
Expand file tree
/
Copy pathnumpy_speedup.py
More file actions
120 lines (95 loc) · 3.18 KB
/
Copy pathnumpy_speedup.py
File metadata and controls
120 lines (95 loc) · 3.18 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Example of vectorizing operations with `numpy`
Blog: https://towardsdatascience.com/the-art-of-speeding-up-python-loop-4970715717c
Blog: https://shihchinw.github.io/2019/03/performance-tips-of-numpy-ndarray.html
"""
import numpy as np
import time
from scipy.fft import rfft2 # depending on version of `numpy` float32 may not output complex64
n_trials = 100
""" Adding two vectors, compare nonvectorized and vectorized operations """
print("\n-- Adding two vectors")
n = 1000
a = np.random.randn(n)
b = np.random.randn(n)
# Nonvectorized
start = time.perf_counter()
for _ in range(n_trials):
c = []
for i in range(len(a)):
c.append(a[i] + b[i])
c = np.array(c)
nonvectorized_time = (time.perf_counter() - start) / n_trials
print(f"Nonvectorized time: {nonvectorized_time} seconds")
# Vectorized
start = time.perf_counter()
for _ in range(n_trials):
c_vec = a + b
vectorized_time = (time.perf_counter() - start) / n_trials
print(f"Vectorized time: {vectorized_time} seconds")
assert np.allclose(c, c_vec)
print(f"Speed-up: {nonvectorized_time / vectorized_time}")
""" Broadcasting """
print("\n-- Broadcasting")
n = 100
a = np.random.randn(n, n)
b = np.random.randn(n) # add a row vector to each row of a
# Nonvectorized
start = time.perf_counter()
for _ in range(n_trials):
c = []
for i in range(len(a)):
c.append(a[i] + b)
c = np.array(c)
nonvectorized_time = (time.perf_counter() - start) / n_trials
print(f"Nonvectorized time: {nonvectorized_time} seconds")
# Vectorized
start = time.perf_counter()
for _ in range(n_trials):
c_vec = a + b[np.newaxis, :]
vectorized_time = (time.perf_counter() - start) / n_trials
print(f"Vectorized time: {vectorized_time} seconds")
assert np.allclose(c, c_vec)
print(f"Speed-up: {nonvectorized_time / vectorized_time}")
""" Apply function along axis (if memory allows) """
print("\n-- Apply function (e.g. fft) along axis")
n = 512
n_signals = 500
a = np.random.randn(n, n_signals)
# Nonvectorized
start = time.perf_counter()
for _ in range(n_trials):
c = []
for i in range(n_signals):
c.append(np.fft.fft(a[:, i]))
c = np.array(c)
nonvectorized_time = (time.perf_counter() - start) / n_trials
print(f"Nonvectorized time: {nonvectorized_time} seconds")
# Vectorized
start = time.perf_counter()
for _ in range(n_trials):
c_vec = np.fft.fft(a, axis=0)
vectorized_time = (time.perf_counter() - start) / n_trials
print(f"Vectorized time: {vectorized_time} seconds")
assert np.allclose(c.T, c_vec)
print(f"Speed-up: {nonvectorized_time / vectorized_time}")
""" Using smaller data types if possible """
print("\n-- Using smaller data types if possible")
# float64
a = np.random.randn(512, 512)
start = time.perf_counter()
for _ in range(n_trials):
# b = np.fft.rfft2(a)
b = rfft2(a)
float64_time = (time.perf_counter() - start) / n_trials
print(f"float64 time: {float64_time} seconds")
# float32
a32 = a.astype(np.float32)
start = time.perf_counter()
for _ in range(n_trials):
# b32 = np.fft.rfft2(a32)
b32 = rfft2(a32)
float32_time = (time.perf_counter() - start) / n_trials
print(f"float32 time: {float32_time} seconds")
assert b32.dtype == np.complex64
print(f"Speed-up: {float64_time / float32_time}")