这里有新鲜出炉的 Python 教程,程序狗速度看过来!
Python 是一种面向对象、解释型计算机程序设计语言,由 Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 1991 年。Python 语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是 C/C++)很轻松地联结在一起。
本文主要介绍了 python 提高性能的方法。具有很好的参考价值,下面跟着小编一起来看下吧
提高性能有如下方法
1、Cython, 用于合并 python 和 c 语言静态编译泛型
2、IPython.parallel,用于在本地或者集群上并行执行代码
3、numexpr,用于快速数值运算
4、multiprocessing,python 内建的并行处理模块
5、Numba,用于为 cpu 动态编译 python 代码
6、NumbaPro,用于为多核 cpu 和 gpu 动态编译 python 代码
为了验证相同算法在上面不同实现上的的性能差异,我们先定义一个测试性能的函数
- def perf_comp_data(func_list, data_list, rep=3, number=1):
- '''Function to compare the performance of different functions.
- Parameters
- func_list : list
- list with function names as strings
- data_list : list
- list with data set names as strings
- rep : int
- number of repetitions of the whole comparison
- number : int
- number ofexecutions for every function
- '''
- from timeit import repeat
- res_list = {}
- for name in enumerate(func_list):
- stmt = name[1] + '(' + data_list[name[0]] + ')'
- setup = "from __main__ import " + name[1] + ','+ data_list[name[0]]
- results = repeat(stmt=stmt, setup=setup, repeat=rep, number=number)
- res_list[name[1]] = sum(results) / rep
- res_sort = sorted(res_list.items(), key = lambda item : item[1])
- for item in res_sort:
- rel = item[1] / res_sort[0][1]
- print ('function: ' + item[0] + ', av. time sec: %9.5f, ' % item[1] + 'relative: %6.1f' % rel)
定义执行的算法如下
- from math import *
- def f(x):
- return abs(cos(x)) ** 0.5 + sin(2 + 3 * x)
对应的数学公式是
生成数据如下
- i=500000
- a_py = range(i)
第一个实现 f1 是在内部循环执行 f 函数,然后将每次的计算结果添加到列表中,实现如下
- def f1(a):
- res = []
- for x in a:
- res.append(f(x))
- return res
当然实现这种方案的方法不止一种,可以使用迭代器或 eval 函数, 我自己加入了使用生成器和 map 方法的测试,发现结果有明显差距,不知道是否科学:
迭代器实现
- def f2(a):
- return [f(x) for x in a]
eval 实现
- def f3(a):
- ex = 'abs(cos(x)) **0.5+ sin(2 + 3 * x)'
- return [eval(ex) for x in a]
生成器实现
- def f7(a):
- return (f(x) for x in a)
map 实现
- def f8(a):
- return map(f, a)
接下来是使用 numpy 的 narray 结构的几种实现
- import numpy as np
- a_np = np.arange(i)
- def f4(a):
- return (np.abs(np.cos(a)) ** 0.5 + np.sin(2 + 3 * a))
- import numexpr as ne
- def f5(a):
- ex = 'abs(cos(a)) ** 0.5 + sin( 2 + 3 * a)'
- ne.set_num_threads(1)
- return ne.evaluate(ex)
- def f6(a):
- ex = 'abs(cos(a)) ** 0.5 + sin(2 + 3 * a)'
- ne.set_num_threads(2)
- return ne.evaluate(ex)
上面的 f5 和 f6 只是使用的处理器个数不同,可以根据自己电脑 cpu 的数目进行修改,也不是越大越好
下面进行测试
- func_list = ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8']
- data_list = ['a_py', 'a_py', 'a_py', 'a_np', 'a_np', 'a_np', 'a_py', 'a_py']
- perf_comp_data(func_list, data_list)
测试结果如下
- function: f8,
- av.time sec: 0.00000,
- relative: 1.0
- function: f7,
- av.time sec: 0.00001,
- relative: 1.7
- function: f6,
- av.time sec: 0.03787,
- relative: 11982.7
- function: f5,
- av.time sec: 0.05838,
- relative: 18472.4
- function: f4,
- av.time sec: 0.09711,
- relative: 30726.8
- function: f2,
- av.time sec: 0.82343,
- relative: 260537.0
- function: f1,
- av.time sec: 0.92557,
- relative: 292855.2
- function: f3,
- av.time sec: 32.80889,
- relative: 10380938.6
发现 f8 的时间最短,调大一下时间精度再测一次
- function: f8,
- av.time sec: 0.000002483,
- relative: 1.0
- function: f7,
- av.time sec: 0.000004741,
- relative: 1.9
- function: f5,
- av.time sec: 0.028068110,
- relative: 11303.0
- function: f6,
- av.time sec: 0.031389788,
- relative: 12640.6
- function: f4,
- av.time sec: 0.053619114,
- relative: 21592.4
- function: f1,
- av.time sec: 0.852619225,
- relative: 343348.7
- function: f2,
- av.time sec: 1.009691877,
- relative: 406601.7
- function: f3,
- av.time sec: 26.035869787,
- relative: 10484613.6
发现使用 map 的性能最高,生成器次之,其他方法的性能就差的很远了。但是使用 narray 数据的在一个数量级,使用 python 的 list 数据又在一个数量级。生成器的原理是并没有生成一个完整的列表,而是在内部维护一个 next 函数,通过一边循环迭代一遍生成下个元素的方法的实现的,所以他既不用在执行时遍历整个循环,也不用分配整个空间,它花费的时间和空间跟列表的大小是没有关系的,map 与之类似,而其他实现都是跟列表大小有关系的。
内存布局
numpy 的 ndarray 构造函数形式为
np.zeros(shape, dtype=float, order='C')
np.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
shape 或 object 定义了数组的大小或是引用了另一个一个数组
dtype 用于定于元素的数据类型,可以是 int8,int32,float8,float64 等等
order 定义了元素在内存中的存储顺序,c 表示行优先,F 表示列优先
下面来比较一下内存布局在数组很大时的差异,先构造同样的的基于 C 和基于 F 的数组,代码如下:
- x = np.random.standard_normal(( 3, 1500000))
- c = np.array(x, order='C')
- f = np.array(x, order='F')
下面来测试性能
- %timeit c.sum(axis=0)
- %timeit c.std(axis=0)
- %timeit f.sum(axis=0)
- %timeit f.std(axis=0)
- %timeit c.sum(axis=1)
- %timeit c.std(axis=1)
- %timeit f.sum(axis=1)
- %timeit f.std(axis=1)
输出如下
- loops,
- best of 3 : 12.1 ms per loop loops,
- best of 3 : 83.3 ms per loop loops,
- best of 3 : 70.2 ms per loop loop,
- best of 3 : 235 ms per loop loops,
- best of 3 : 7.11 ms per loop loops,
- best of 3 : 37.2 ms per loop loops,
- best of 3 : 54.7 ms per loop loops,
- best of 3 : 193 ms per loop
可知,C 内存布局要优于 F 内存布局
并行计算
未完,待续。。。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持 PHPERZ!
(adsbygoogle = window.adsbygoogle || []).push({});
来源: http://www.phperz.com/article/17/0623/332901.html