python代码优化笔记,cython等

本文主要是整理看过的不错的文章,具体内容请参考对应链接

[size=large][color=blue]1、内存[/color][/size]
__slots__使得不用为每个实例分配一个__dict__,对于数量巨多的小对象内存优化显著(降到1/4)
使用cython, 小对象降到使用__slots__的1/2不是问题,参见:[url]http://cython.readthedocs.io/en/latest/src/reference/extension_types.html[/url]


[size=large][color=blue]2、速度[/color][/size]
各种小的注意事项:[url]https://wiki.python.org/moin/PythonSpeed/PerformanceTips[/url]
[list]
[*]循环外c=time.time; 循环内用 c() 替代带点的操作 time.time()
[*]将import写在需要多次调用的函数外面,尤其是函数很简短时
[/list]

知乎上的讨论numba等:[url]https://www.zhihu.com/question/24695645[/url]

通过Python-C-Api,Ctypes,swig或cython等调用c实现(很不错的文章):[url]https://segmentfault.com/a/1190000000479951[/url]


[size=large][color=blue]3、cython相关资料[/color][/size]
[color=red]注意:[/color]pydev代码提示,对于编译后的动态链接库,需要在Forced Builtins手动添加.so的变量名(对于openCV为cv2,对于自己的模块,比如LC.moduleA.so, 需要添加LC.moduleA)[url]http://www.pydev.org/manual_101_interpreter.html[/url]

很赞的第三方(numpy,直接操作numpy的指针等)[b]示例[/b]:
[url]http://notes-on-cython.readthedocs.io/en/latest/std_dev.html[/url]

[b]cython官网[/b]的一些文档:
入门(很简单):[url]http://cython.readthedocs.io/en/latest/src/quickstart/build.html[/url]
类:[url]http://cython.readthedocs.io/en/latest/src/reference/extension_types.html[/url]
str相关(注意在cython的class中是可以通过cdef 定义python中的类型的,比如str):[url]http://cython.readthedocs.io/en/latest/src/tutorial/strings.html[/url]
使用openMP:[url]http://cython.readthedocs.io/en/latest/src/userguide/parallelism.html?highlight=loop[/url]
cython结合numpy:[url]http://cython.readthedocs.io/en/latest/src/tutorial/numpy.html[/url]
Typed Memoryviews(新的传递numpy数组的方式,更清晰,但速度稍慢):[url]http://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html[/url]

编译选项(string的转换策略,是否启用boundscheck等):[url]http://cython.readthedocs.io/en/latest/src/reference/compilation.html#compiler-directives[/url]

增量编程(主代码在py文件中, 可以转为c的定义在pyx文件中):[url]http://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html[/url]

代码效率分析:
cython -a --cplus code.pyx #无需指定额外cpp和h文件路径

参考自:[url]https://zhuanlan.zhihu.com/p/24311879[/url]


[size=large][color=blue]4、 多线程,多进程[/color][/size]
对比测试,GIL知识参见:[url]http://stackoverflow.com/questions/10154487/how-to-get-a-faster-speed-when-using-multi-threading-in-python[/url]

[color=blue][b]多进程:[/b][/color] 可以多核并行执行,但是参数是通过序列化和反序列化在进程间传递的,因而,多进程适用于输入,输出参数都很小,但是采用纯python代码进行大量运算的情况

[color=blue][b]多线程:[/b][/color] 由于GIL(Global Interpreter Lock)的存在,只适用于IO密集型程序(不受GIL限制的扩展程序除外,比如在cython中使用nogil);CPU密集型程序反而会导致性能下降(都需要python解释器进行解释,但只有获得GIL的线程才能够执行)。。。

[color=blue][b]心得:[/b][/color] [b]先启动无需GIL的线程,再启动需要GIL的线程[/b]
需要两个线程,一个是第三方GPU程序,耗时较长,另一个是python程序,耗时稍短,如果一个在主线程中执行,一个通过Thread在另外的线程中执行,将哪一个放在新的线程中? 答案是GPU程序,因为如果将python程序放在另外一个线程中,就需要先启动该线程,该线程释放GIL需要一定的时间,会导致GPU程序的启动时间滞后。 即使这两个程序都放在新的线程中执行,也必须先启动GPU程序,因为GPU程序会立即释放GIL,而python程序会先运行一段时间再释放GIL,只有释放了GIL,主线程中启动GPU程序的命令才会被执行

[size=large][color=blue]5、编码问题[/color][/size]
关于python2为什么不推荐使用[i]sys.setdefaultencoding("utf-8")[/i]:
[url]http://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script[/url]
个人觉着只要能解决问题就行,毕竟python3默认就是utf-8了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值