用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 ...
随机推荐
- Linux内核设计第八周 ——进程的切换和系统的一般执行过程
Linux内核设计第八周 ——进程的切换和系统的一般执行过程 第一部分 知识点总结 第二部分 实验部分 1.配置实验环境,确保menu内核可以正常启动 2.进入gdb调试,在shedule和conte ...
- cocos2dx 3.x(在Mac平台下利用Eclipse打包安卓apk安装包详细教程)
最近在学习cocos2dx在MAC上如何打包apk,今天先把安装JDK和ANT的过程记来. 首先,打开终端,输入"java -version" 点击回车后,出现如下提示: 我们的M ...
- 【转】Ubuntu防火墙设置
1.安装 sudo apt-get install ufw 2.启用 sudo ufw enable sudo ufw default deny 运行以上两条命令后,开启了防火墙,并在系统启动时自动开 ...
- 获取图片中感兴趣区域的信息(Matlab实现)
内容提要 如果一幅图中只有一小部分图像你感兴趣(你想研究的部分),那么截图工具就可以了,但是如果你想知道这个区域在原图像中的坐标位置呢? 这可是截图工具所办不到的,前段时间我就需要这个功能,于是将其用 ...
- php操作Mysql 以及封装常用的函数 用外连接连接3个表的案例
<?php header("content-type;text/html;charset=utf-8"); //数据库连接define('DB_HOST','localhos ...
- 多列布局——Columns
为了能在Web页面中方便实现类似报纸.杂志那种多列排版的布局,W3C特意给CSS3增加了一个多列布局模块(CSS Multi Column Layout Module).它主要应用在文本的多列布局方面 ...
- 原生js与css3结合的电风扇
最近学习了css3,就琢磨做些东西练练手,下面是自己写的一个电风扇,使用了原生js中的定时器和css3的一些属性 <!doctype html> <html lang="e ...
- 16. 星际争霸之php设计模式--组合模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 用.Net Mage工具更新WPF ClickOnce应用程序部署清单
Wpf程序在iis上发布后的文件结构.不像asp.net可以直接在服务器上修改网站目录的web.config文件或其他文件,wpf发布的结构是.application文件和一个Application ...
- 嵌入式Linux内核制作【转】
本文转载自:http://blog.csdn.net/coding__madman/article/details/51291316 1. Linux体系结构 从整体上来分,linux可以分为User ...