有用过 Cython 的同学,说下实际效果吗?兼容性方面怎么样?
7 条回复 • 2017-10-14 09:40:09 +08:00
|
1
hanzhichao2000 2017 年 10 月 13 日
Ref: http://notes-on-cython.readthedocs.io/en/latest/std_dev.html
Example: def pyStdDev(a): mean = sum(a) / len(a) return math.sqrt((sum(((x - mean)**2 for x in a)) / len(a))) In summary: ================= ============ ================== ===================== Method Time (ms) Compared to Python Compared to Numpy ================= ============ ================== ===================== Pure Python 183 x1 x0.03 Numpy 5.97 x31 x1 Naive Cython 7.76 x24 x0.8 Optimised Cython 2.18 x84 x2.7 Cython calling C 2.22 x82 x2.7 ================= ============ ================== ===================== |
|
2
rocksolid 2017 年 10 月 13 日
@hanzhichao2000 Numpy 真是神器
|
|
3
hanzhichao2000 2017 年 10 月 13 日
如果涉及数值计算的话,numba 可能用起来更方便,cython 毕竟还要显性配置些东西
|
|
4
justou 2017 年 10 月 13 日
可以提升至 C/C++效率, 兼容性是 C/C++的兼容性(毕竟 cython 转译出来的就是.c/.cpp)
|
|
5
janxin 2017 年 10 月 13 日
|
|
6
mckelvin 2017 年 10 月 13 日
首先你要清楚程序的瓶颈在什么,这一阶段可以用 pyflame ( https://github.com/uber/pyflame ), systemtap( https://docs.python.org/3/howto/instrumentation.html) 等工具导出 flamegraph。如果是语言实现引起的瓶颈,那可以考虑上 Cython. 如果你的程序用标准的 cpython 运行,那 Cython 兼容性很好。
一般来说 Python 的 socket 慢,CPU 密集型计算也慢,这种情况就特别适合用 Cython 做胶水层,把瓶颈部分用 C 或 C++实现。至于能快多少,场景不同,实现也不同,不能一句话告诉你有多块,主要取决于你的 C++ / C 代码有多快。 |
|
7
abcdabcd987 2017 年 10 月 14 日
|
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide