学习 使用 python库 对程序进行加速
如果使用常规数据结构和操作进行程序编译会发现速度很慢,尤其是在对大量数据进行处理的时候。
如果使用python库进行编译会发现运行速度有显著提升。
测速使用ipython的magic方法
In [1]: loops = 25000000
In [2]: from math import *
In [3]: a = range(1, loops)
In [4]: def f(x):
...: return 3 * log(x) + cos(x) ** 2
In [5]: %timeit r = [f(x) for x in a]
28.3 s ± 2.27 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [6]:
In [6]: import numpy as np
In [7]: a = np.arange(1, loops)
In [8]: %timeit r = 3 * np.log(a) + np.cos(a) ** 2
1.83 s ± 152 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [9]: import numexpr as ne
In [10]: ne.set_vml_num_threads(1)
IIn [11]: f = '3 * log(a) + cos(a)**2'
In [12]: %timeit r = ne.evaluate(f)
217 ms ± 6.85 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [13]: ne.set_vml_num_threads(4)
In [14]: %timeit r = ne.evaluate(f)
181 ms ± 5.62 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)