用cython提升python的性能
Boosting performance with Cython
Even with my old pc (AMD Athlon II, 3GB ram), I seldom run into performance issues when running vectorized code. But unfortunately there are plenty of cases where that can not be easily vectorized, for example the drawdown function. My implementation of such was extremely slow, so I decided to use it as a test case for speeding things up. I'll be using the SPY timeseries with ~5k samples as test data. Here comes the original version of my drawdown function (as it is now implemented in the TradingWithPython library)
|
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
|
def drawdown(pnl): """ calculate max drawdown and duration Returns: drawdown : vector of drawdwon values duration : vector of drawdown duration """ cumret = pnl highwatermark = [0] idx = pnl.index drawdown = pd.Series(index = idx) drawdowndur = pd.Series(index = idx) for t in range(1, len(idx)) : highwatermark.append(max(highwatermark[t-1], cumret[t])) drawdown[t]= (highwatermark[t]-cumret[t]) drawdowndur[t]= (0 if drawdown[t] == 0 else drawdowndur[t-1]+1) return drawdown, drawdowndur%timeit drawdown(spy)1 loops, best of 3: 1.21 s per loop |
Hmm 1.2 seconds is not too speedy for such a simple function. There are some things here that could be a great drag to performance, such as a list *highwatermark* that is being appended on each loop iteration. Accessing Series by their index should also involve some processing that is not strictly necesarry. Let's take a look at what happens when this function is rewritten to work with numpy data
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def dd(s):# ''' simple drawdown function ''' highwatermark = np.zeros(len(s)) drawdown = np.zeros(len(s)) drawdowndur = np.zeros(len(s)) for t in range(1,len(s)): highwatermark[t] = max(highwatermark[t-1], s[t]) drawdown[t] = (highwatermark[t]-s[t]) drawdowndur[t]= (0 if drawdown[t] == 0 else drawdowndur[t-1]+1) return drawdown , drawdowndur%timeit dd(spy.values)10 loops, best of 3: 27.9 ms per loop |
Well, this is much faster than the original function, approximately 40x speed increase. Still there is much room for improvement by moving to compiled code with cython Now I rewrite the dd function from above, but using optimisation tips that I've found on the cython tutorial .
用cython提升python的性能的更多相关文章
- 七个可以提升python程序性能的好习惯,你知道吗?
掌握一些技巧,可尽量提高Python程序性能,也可以避免不必要的资源浪费.今天就为大家带来七个可以提升python程序性能的好习惯,赶快来学习吧:. 1.使用局部变量 尽量使用局部变量代替全局变量:便 ...
- 7个提升Python程序性能的好习惯
原文作者:爱coding,会编程的核电工程师. 个人博客地址:zhihu.com/people/zhong-yun-75-63 掌握一些技巧,可尽量提高Python程序性能,也可以避免不必要的资源浪费 ...
- 【python 应用之四】提升 Python 运行性能的 7 个习惯
大家都知道艺赛旗的 RPA 依赖于 python 语言.因此我们可以掌握一些技巧,可尽量提高 Python 程序性能,也可以避免不必要的资源浪费.1.使用局部变量 尽量使用局部变量代替全局变量:便于维 ...
- [转] Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- Python代码性能优化技巧
摘要:代码优化能够让程序运行更快,可以提高程序的执行效率等,对于一名软件开发人员来说,如何优化代码,从哪里入手进行优化?这些都是他们十分关心的问题.本文着重讲了如何优化Python代码,看完一定会让你 ...
- Python 代码性能优化技巧(转)
原文:Python 代码性能优化技巧 Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化. ...
- Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- 用Cython加速Python程序以及包装C程序简单测试
用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...
- psutil 是因为该包能提升 memory_profiler 的性能
python 性能分析入门指南 一点号数据玩家昨天 限时干货下载:添加微信公众号"数据玩家「fbigdata」" 回复[7]免费获取[完整数据分析资料!(包括SPSS.SAS.SQ ...
随机推荐
- C#中ref和out的使用与区别
C#中ref关键字和out关键字所实现的功能差不多,都是指定一个形参按照引用传递而不是实参的副本传递.但是二者适用场景还是有些区别的:out适合用在需要retrun多个返回值的地方,而ref则适合用在 ...
- linux多核cpu下的负载查看
linux下使用top命令或uptime命令 单核cpu下,负载超过0.7即意味着瓶颈,多核cpu下按核数*0.7计算负载 如2核,1.4可能即意味着负载较吃力了 查看核数 grep 'model n ...
- smaller programs should improve performance
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In this section, we l ...
- [knowledge][basic][hardware] 内存的硬件结构(转)
写的并不太易懂,但概念性的科普已足够. 原文地址:http://blog.csdn.net/miaomao1989/article/details/51508195 Memory中的Channel/R ...
- JS中的_proto_(2)
function God(){} function Foo(){ this.name="Foo~~"; } Foo.prototype = new God(); function ...
- UML聚合与组合
http://www.cnblogs.com/shanwenbin/archive/2012/10/24/2737229.html UML聚合与组合 2012-10-24 15:35 by DayDa ...
- Maven-008-Nexus 私服部署发布报错 Failed to deploy artifacts: Failed to transfer file: ... Return code is: 4XX, ReasonPhrase: ... 解决方案
我在部署构件至 maven nexus 私服时,有时会出现 Failed to deploy artifacts: Failed to transfer file: ... Return code i ...
- Slip.js(移动端跟随手指滑动组件,零依赖)
Slip.js可用于移动端滑动banner,移动端整屏滑动等效果,个人觉得用于移动端滑动banner比较好,不会和iScroll.js起冲突,因为它不依赖任何其它的js库. Html: <!do ...
- Python开发【第三章】:Python的文件操作
一.文件操作模式概述 1.打开文件的模式: r, 只读模式[默认] w,只写模式[不可读:不存在则创建:存在则删除内容:] a, 追加模式[不可读:不存在则创建:存在则只追加内容:] 2." ...
- centos 下建用户 shell编程
useradd 用户名 passwd 用户名 cat /etc/passwd 查看用户信息 删除用户 userdel -r 加一个 -r 表示把用户及用户的主目录都删除 su 切换用户 sud ...