-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.Numpy和原生Python用于数组计算的性能对比
230 lines (139 loc) · 2.88 KB
/
01.Numpy和原生Python用于数组计算的性能对比
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
### Numpy和原生Python用于数组计算的性能对比
需求:
* 实现两个数组的加法;
* 数组A是1--N数字的平方;
* 数组B是1--N数字的立方
### 引入Numpy的包
```python
import numpy as np
```
```python
np.__version__
```
'1.18.1'
### 使用Python原生语法实现对比
```python
def python_sum(n):
'''
Python实现数组的加法
n:数组的长度
'''
a = [i**2 for i in range(n)]
b = [i**3 for i in range(n)]
c = []
for i in range(n):
c.append(a[i] + b[i])
return c
```
```python
#测试一下
python_sum(10)
```
[0, 2, 12, 36, 80, 150, 252, 392, 576, 810]
### 使用Numpy实现
```python
def numpy_sum(n):
'''
numpy实现数组的加法
n:数组的长度
'''
a = np.arange(n) ** 2
b = np.arange(n) ** 3
return a+b#对应元素相加
```
```python
#测试一下
numpy_sum(10)
```
array([ 0, 2, 12, 36, 80, 150, 252, 392, 576, 810], dtype=int32)
### 性能对比:执行1000次
```python
%timeit python_sum(1000)
```
1.63 ms ± 492 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
```python
%timeit numpy_sum(1000)
```
11.7 µs ± 705 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
### 性能对比:执行10W次
```python
%timeit python_sum(100000)
```
141 ms ± 22.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
```python
%timeit numpy_sum(100000)
```
524 µs ± 22 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
### 性能对比:执行1000W次
```python
%timeit python_sum(1000 * 10000)
```
14.5 s ± 903 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```python
%timeit numpy_sum(1000 * 10000)
```
184 ms ± 1.94 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
### 绘制性能对比图
```python
pytimes = [1.06*1000, 104*1000, 10.4*1000*1000]
nptimes = [9.16, 424, 114*1000]
```
```python
import pandas as pd
```
```python
df = pd.DataFrame({
"pytimes":pytimes,
"nptimes":nptimes,
})
```
```python
df
```
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>pytimes</th>
<th>nptimes</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1060.0</td>
<td>9.16</td>
</tr>
<tr>
<th>1</th>
<td>104000.0</td>
<td>424.00</td>
</tr>
<tr>
<th>2</th>
<td>10400000.0</td>
<td>114000.00</td>
</tr>
</tbody>
</table>
</div>
```python
%matplotlib inline
df.plot.bar()
```
<matplotlib.axes._subplots.AxesSubplot at 0x1a6821dc388>
![png](output_24_1.png)
```python
```