返回介绍

Recommendations for optimizing Python code

发布于 2025-02-25 23:44:03 字数 2036 浏览 0 评论 0 收藏 0

  • Does a reliable fast implementiaont already exist? If so, consider using that
  • Start with a numpy/python prototype - if this is fast enough, stop
  • See if better use of vectoriazaiton via numpy will help
  • Moving to native code:
    • Most Python devleopers will use Cython as the tool of choice. Cython can also be used to access/wrap C/C++ code
    • JIT compilation with numba is improving fast and may become competitive with Cython in the near future
    • If the function is “minimal”, it is usually worth considering numexpr because there is almost no work to be done
    • Use C/C++/Fortran if you are fluent in those languages - you have seen how to call these functions from Python
  • If appropriate, consider parallelization (covered in later session)
  • As you optimize your code, remmeber:
    • Check that is is giving correct results!
    • Profile often - it is very hard to preidct the effect of an optimizaiton in general
    • Remember that your time is precious - stop when fast enough
    • If getting a bigger, faster machine will sovle the problem, that is sometimes the best solution
%load_ext version_information

%version_information numpy, scipy, numexpr, numba, numbapro, parakeet, cython, f2py,
SoftwareVersion
Python2.7.9 64bit [GCC 4.2.1 (Apple Inc. build 5577)]
IPython3.1.0
OSDarwin 13.4.0 x86_64 i386 64bit
numpy1.9.2
scipy0.15.1
numexpr2.3.1
numba0.17.0
numbapro0.17.1
parakeet0.23.2
cython0.22
f2pyf2py
Thu Apr 09 09:52:28 2015 EDT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文