前期从以下九个方面讨论了加速Python的具体方法,一共24个,每个都带有优化前后的对比,非常实用。

  1. 分析代码运行时间
  2. 加速查找
  3. 加速循环
  4. 加速函数
  5. 实用标准库加速
  6. Numpy向量化加速
  7. 加速Pandas
  8. Dask加速
  9. 多线程多进程加速

我在此基础上主要美化了编辑,方便读者更容易阅读学习。“一 、分析代码运行时间”1 测算代码单次运行时间
平凡法:


pytorch for 循环 加速 python 循环加速_pytorch for 循环 加速


快捷法(Jupyter):


pytorch for 循环 加速 python 循环加速_python 循环加速_02


2 测算代码重复执行多次平均用时
平凡法:


pytorch for 循环 加速 python 循环加速_python 循环加速_03


快捷法(Jupyter):


pytorch for 循环 加速 python 循环加速_多进程_04


3 按调用函数分析代码运行时间
平凡法:


pytorch for 循环 加速 python 循环加速_多进程_05


快捷法(Jupyter):


pytorch for 循环 加速 python 循环加速_Python_06


4 按行分析代码运行时间
平凡法:


pytorch for 循环 加速 python 循环加速_运行时间_07


快捷法(Jupyter):


pytorch for 循环 加速 python 循环加速_运行时间_08


“二、加速你的查找”5 用set而非list进行in查找
低速法:


pytorch for 循环 加速 python 循环加速_python 循环加速_09


高速法:


pytorch for 循环 加速 python 循环加速_运行时间_10


6 用dict而非两个list进行匹配查找
低速法:


pytorch for 循环 加速 python 循环加速_运行时间_11


高速法:


pytorch for 循环 加速 python 循环加速_python 循环加速_12


“三、加速你的循环”7 优先使用for循环而不是while循环
低速法:


pytorch for 循环 加速 python 循环加速_多进程_13


高速法:


pytorch for 循环 加速 python 循环加速_多进程_14


8 循环体中避免重复运算
低速法:


pytorch for 循环 加速 python 循环加速_Python_15


高速法:


pytorch for 循环 加速 python 循环加速_Python_16


“四、加速你的函数”9、用缓存机制加速递归函数
低速法:


pytorch for 循环 加速 python 循环加速_多进程_17


高速法:


pytorch for 循环 加速 python 循环加速_Python_18


10、用循环取代递归
低速法:


pytorch for 循环 加速 python 循环加速_多进程_17


高速法:


pytorch for 循环 加速 python 循环加速_pytorch for 循环 加速_20


11、 使用Numba加速Python函数
低速法:


pytorch for 循环 加速 python 循环加速_pytorch for 循环 加速_21


高速法:


pytorch for 循环 加速 python 循环加速_python 循环加速_22


“五、使用标准库函数进行加速”12、使用collections.Counter类加速计数
低速法:


pytorch for 循环 加速 python 循环加速_pytorch for 循环 加速_23


高速法:


pytorch for 循环 加速 python 循环加速_多进程_24


13、使用collections.ChainMap加速字典合并
低速法:


pytorch for 循环 加速 python 循环加速_pytorch for 循环 加速_25


高速法:


pytorch for 循环 加速 python 循环加速_运行时间_26


“六、使用numpy向量化进行加速”14、使用np.array代替list
低速法:


pytorch for 循环 加速 python 循环加速_python 循环加速_27


高速法:


pytorch for 循环 加速 python 循环加速_Python_28


15、使用np.ufunc代替math.func
低速法:


pytorch for 循环 加速 python 循环加速_python 循环加速_29


高速法:


pytorch for 循环 加速 python 循环加速_Python_30


16、使用np.where代替if
低速法:


pytorch for 循环 加速 python 循环加速_多进程_31


高速法:


pytorch for 循环 加速 python 循环加速_多进程_32


“七、加速你的Pandas”
低速法:


pytorch for 循环 加速 python 循环加速_多进程_33


高速法:


pytorch for 循环 加速 python 循环加速_运行时间_34


18、避免动态改变DataFrame的行数
低速法:


pytorch for 循环 加速 python 循环加速_运行时间_35


高速法:


pytorch for 循环 加速 python 循环加速_Python_36


19、使用csv文件读写代替xlsx文件读写
低速法:


pytorch for 循环 加速 python 循环加速_Python_37


高速法:


pytorch for 循环 加速 python 循环加速_多进程_38


20、使用pandas多进程工具pandarallel
低速法:


pytorch for 循环 加速 python 循环加速_多进程_39


高速法:


pytorch for 循环 加速 python 循环加速_python 循环加速_40


“八、使用Dask进行加速”21、使用dask加速dataframe
低速法:


pytorch for 循环 加速 python 循环加速_运行时间_41


高速法:


pytorch for 循环 加速 python 循环加速_运行时间_42


22、使用dask.delayed应用多进程加速
低速法:


pytorch for 循环 加速 python 循环加速_python 循环加速_43


高速法:


pytorch for 循环 加速 python 循环加速_多进程_44


“九、应用多线程多进程加速”23、使用多线程提升IO密集任务效率
低速法:


pytorch for 循环 加速 python 循环加速_多进程_45


高速法:


pytorch for 循环 加速 python 循环加速_运行时间_46


24、使用多进程提升CPU密集任务效率
低速法:


pytorch for 循环 加速 python 循环加速_pytorch for 循环 加速_47


高速法:


pytorch for 循环 加速 python 循环加速_Python_48