多种方式进行 Python 性能优化对比。
测试代码
| 1 | def calEmbDistance(emb1, emb2): | 
优化方式
| 1 | np.dot 替换 np.sum(np.square) | 
运算速度对比:
| 1 | 原始:1.983642 | 
总结
总的来说,cython 优化最好,如果加上类型的静态定义,会更快,而 numba 和 numexpr 则并没有加速,反而更慢,np.dot 最方便,无需做任何其他配置,能使用的时候一定要使用。
多种方式进行 Python 性能优化对比。
| 1 | def calEmbDistance(emb1, emb2): | 
| 1 | np.dot 替换 np.sum(np.square) | 
运算速度对比:
| 1 | 原始:1.983642 | 
总的来说,cython 优化最好,如果加上类型的静态定义,会更快,而 numba 和 numexpr 则并没有加速,反而更慢,np.dot 最方便,无需做任何其他配置,能使用的时候一定要使用。