将 python 文件编译为 cython 会加快程序速度吗?
Will compiling python files to cython speed up the program?
如果我将 python 文件编译为 cython,速度会有所提高吗?或者我是否需要在 cython 中重写我的代码才能真正看到改进?
我在下面这样做。
python convert_to_cython.py build_ext --inplace
虽然这个问题很宽泛,但笼统地说是。它确实加快了您的代码速度,有时速度达到 100。
作为参考,Cython 文档说,我引用
However, for performance critical code, it is often helpful to add static type declarations, as they will allow Cython to step out of the dynamic nature of the Python code and generate simpler and faster C code - sometimes faster by orders of magnitude
像 C/C++ 这样的语言速度更快的主要原因是它们创建了 machine-dependent 汇编语言来调整所有依赖于硬件的优化。这是使用编译器实现的,主要是因为
之类的东西很少
- 静态类型变量
- 循环展开
- 分支预测
等..
现在Cython 广泛使用了重要特性之一静态类型变量。由于 python 变量是无类型的,而 C 变量不是,Cython 可以让用户灵活地静态固定其变量的类型。
在 Cython 文档 website 中,他们展示了如何通过提及类型实际产生 35% 更快的性能。
注意 但是我最后要说的是,在 Cython 中转换 Python 代码时要小心,因为您可能会使用一些 frameworks/APIs 在您的项目中不支持 Cython。有时,即使您将代码转换为 Cython,也几乎不会改变内部的任何内容。所以这一切都取决于你的代码。
因此,首先确保您的 Python 代码在 Cython 中的完全可移植性,并检查这是否绝对必要。
编辑 1 还有一件事是,在 Cython 中转换您的代码会降低其可读性,因此请注意这一点。
再次引用 Cython 文档
It must be noted, however, that type declarations can make the source code more verbose and thus less readable. It is therefore discouraged to use them without good reason, such as where benchmarks prove that they really make the code substantially faster in a performance critical section
编辑 2 回答问题 或者我是否需要用 cython 重写我的代码才能真正看到改进?
否 因为 Cython 编译器会为您完成所有事情。
The Cython compiler will convert it into C code which makes equivalent calls to the Python/C API.
As Cython can accept almost any valid python source file, one of the hardest things in getting started is just figuring out how to compile your extension.
有关详细信息,请访问 this
就目前而言,这个问题过于宽泛,因为答案在很大程度上取决于您的 Python 代码的外观。没有看到它,或者至少对它的作用有一些了解,不可能给你一个直接的答案。
如果您的代码主要调用其他已编译库中的函数,那么您很可能看不到任何改进,甚至运行时间更慢。例如,SO 上有 lots of questions 来自无法理解为什么 Cython 不能神奇地加速他们的 numpy 代码的人。但是,嵌套 Python for
循环之类的东西可能是使用 Cython 加速的非常好的候选者。
您的第一步应该始终是剖析您的代码(例如使用优秀的line_profiler
)。一旦你确定了瓶颈在哪里,你就可以考虑如何加快它们的速度,可能是通过用 Cython 重写它们。在 Cython 中重写所有现有 Python 代码几乎肯定是浪费时间。
如果我将 python 文件编译为 cython,速度会有所提高吗?或者我是否需要在 cython 中重写我的代码才能真正看到改进?
我在下面这样做。
python convert_to_cython.py build_ext --inplace
虽然这个问题很宽泛,但笼统地说是。它确实加快了您的代码速度,有时速度达到 100。
作为参考,Cython 文档说,我引用
However, for performance critical code, it is often helpful to add static type declarations, as they will allow Cython to step out of the dynamic nature of the Python code and generate simpler and faster C code - sometimes faster by orders of magnitude
像 C/C++ 这样的语言速度更快的主要原因是它们创建了 machine-dependent 汇编语言来调整所有依赖于硬件的优化。这是使用编译器实现的,主要是因为
之类的东西很少- 静态类型变量
- 循环展开
- 分支预测
等..
现在Cython 广泛使用了重要特性之一静态类型变量。由于 python 变量是无类型的,而 C 变量不是,Cython 可以让用户灵活地静态固定其变量的类型。
在 Cython 文档 website 中,他们展示了如何通过提及类型实际产生 35% 更快的性能。
注意 但是我最后要说的是,在 Cython 中转换 Python 代码时要小心,因为您可能会使用一些 frameworks/APIs 在您的项目中不支持 Cython。有时,即使您将代码转换为 Cython,也几乎不会改变内部的任何内容。所以这一切都取决于你的代码。
因此,首先确保您的 Python 代码在 Cython 中的完全可移植性,并检查这是否绝对必要。
编辑 1 还有一件事是,在 Cython 中转换您的代码会降低其可读性,因此请注意这一点。
再次引用 Cython 文档
It must be noted, however, that type declarations can make the source code more verbose and thus less readable. It is therefore discouraged to use them without good reason, such as where benchmarks prove that they really make the code substantially faster in a performance critical section
编辑 2 回答问题 或者我是否需要用 cython 重写我的代码才能真正看到改进?
否 因为 Cython 编译器会为您完成所有事情。
The Cython compiler will convert it into C code which makes equivalent calls to the Python/C API.
As Cython can accept almost any valid python source file, one of the hardest things in getting started is just figuring out how to compile your extension.
有关详细信息,请访问 this
就目前而言,这个问题过于宽泛,因为答案在很大程度上取决于您的 Python 代码的外观。没有看到它,或者至少对它的作用有一些了解,不可能给你一个直接的答案。
如果您的代码主要调用其他已编译库中的函数,那么您很可能看不到任何改进,甚至运行时间更慢。例如,SO 上有 lots of questions 来自无法理解为什么 Cython 不能神奇地加速他们的 numpy 代码的人。但是,嵌套 Python for
循环之类的东西可能是使用 Cython 加速的非常好的候选者。
您的第一步应该始终是剖析您的代码(例如使用优秀的line_profiler
)。一旦你确定了瓶颈在哪里,你就可以考虑如何加快它们的速度,可能是通过用 Cython 重写它们。在 Cython 中重写所有现有 Python 代码几乎肯定是浪费时间。