The Speed of Numpy

Overview

实现算法时,越发觉得,Numpy对效率的影响特别大,所以再次学习。
这里主要是进行数组运算速度的对比,对原文代码稍加改进,更直观地看到速度上的差别。

Code
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

import sys
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
def numpysum(n):
a = np.arange(n)**2
b = np.arange(n)**3
c =a + b
return c
def pythonsum(n):
# 这里由于源码为Python2的,range的用法可能有变
# 直接运行报错TypeError: 'range' object does not support item assignment
# 故转化为列表
a = list(range(n))
b = list(range(n))
c = []
for i in range(len(a)):
a[i] = i ** 2
b[i] = i ** 3
c.append(a[i] + b[i])
return c
# prt表示是否打印结果
def printest(func, size, prt=True):
start = datetime.now()
c = func(size)
delta = datetime.now() - start
if prt==True:
print("The last 2 elements of the sum ", c[-2:])
print('Elapsed time in microsecondas ', delta.microseconds)
return delta.microseconds
# 用于作n-time图
def timeplot():
pts = []
x = []
for i in range(100, 10000, 100):
t_numpy = printest(numpysum, i, prt=False)
t_python = printest(pythonsum, i, prt=False)
pts.append([t_numpy, t_python])
x.append(i)
plt.plot(x, pts)
plt.legend(['Numpy', 'Python'])
plt.show()
if __name__=='__main__':
size = int(sys.argv[1])
print('Numpysum...')
printest(numpysum, size)
print('Pythonsum...')
printest(pythonsum, size)
timeplot()



运行:

python Speed.py 10000

输出:
>
Numpysum…
The last 2 elements of the sum [999500079996 999800010000]
Elapsed time in microsecondas 1299
Pythonsum…
The last 2 elements of the sum [999500079996, 999800010000]
Elapsed time in microsecondas 18202

可以看到,相比pure python, Numpy在大规模的运算上具有极高的效率。

Supplement

关于做图的补充:

1
2
3
4
5
6
7
8
9

import matplotlib.pyplot as plt


l = [[1, 2], [3, 4], [5, 6]]
plt.plot([1, 2, 3], l)
plt.legend(['a', 'b'])
plt.show()


输出:

Reference

NumPy Beginner’s Guide [Second Edition] Page19

击蒙御寇