作者:梁云1991,来源: Python与算法之美
一,分析代码运行时间
第1式,测算代码运行时间
平凡方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75418-1.png)
快捷方法(jupyter环境)
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75419-2.png)
第2式,测算代码多次运行平均时间
平凡方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75420-3.png)
快捷方法(jupyter环境)
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75421-4.png)
第3式,按调用函数分析代码运行时间
平凡方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75422-5.png)
快捷方法(jupyter环境)
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75423-6.png)
第4式,按行分析代码运行时间
平凡方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75425-7.png)
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75426-8.png)
快捷方法(jupyter环境)
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75427-9.png)
二,加速你的查找
第5式,用set而非list进行查找
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75428-10.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75429-11.png)
第6式,用dict而非两个list进行匹配查找
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75430-12.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75431-13.png)
三,加速你的循环
第7式,优先使用for循环而不是while循环
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75432-14.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75433-15.png)
第8式,在循环体中避免重复计算
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75434-16.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75435-17.png)
四,加速你的函数
第9式,用循环机制代替递归函数
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75436-18.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75437-19.png)
第10式,用缓存机制加速递归函数
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75438-20.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75439-21.png)
第11式,用numba加速Python函数
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75440-22.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75441-23.png)
五,使用标准库函数进行加速
第12式,使用collections.Counter加速计数
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75442-24.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75443-25.png)
第13式,使用collections.ChainMap加速字典合并
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75444-26.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75445-27.png)
六,使用numpy向量化进行加速
第14式,使用np.array代替list
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75446-28.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75447-29.png)
第15式,使用np.ufunc代替math.func
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75448-30.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75449-31.png)
第16式,使用np.where代替if
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75450-32.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75451-33.png)
七,加速你的Pandas
第17式,使用np.ufunc函数代替applymap
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75452-34.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75453-35.png)
第18式,使用预分配存储代替动态扩容
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75454-36.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75455-37.png)
第19式,使用csv文件读写代替excel文件读写
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75456-38.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75457-39.png)
第20式,使用pandas多进程工具pandarallel
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75458-40.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75459-41.png)
八,使用Dask进行加速
第21式,使用dask加速dataframe
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75460-42.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75461-43.png)
第22式,使用dask.delayed进行加速
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75462-44.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75463-45.png)
九,应用多线程多进程加速
第23式,应用多线程加速IO密集型任务
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75464-46.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75465-47.png)
第24式,应用多进程加速CPU密集型任务
低速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75466-48.png)
高速方法
![](http://xilinx.eetrend.com/files/2019-07/wen_zhang_/100044281-75467-49.png)
文章转载自:机器学习算法与Python学习