TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

High Performance Python Extensions: Part 1

72 点作者 JohnnyLee超过 10 年前

7 条评论

syllogism超过 10 年前
Here&#x27;s how I would write this, in Cython using &quot;pure C&quot; arrays:<p><a href="https://gist.github.com/syllog1sm/3dd24cc8b0ad925325e1" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;syllog1sm&#x2F;3dd24cc8b0ad925325e1</a><p>It&#x27;s getting 18,000 steps&#x2F;second, in the same ballpark as your C code.<p>I prefer to &quot;write C in Cython&quot;, because I find it easier to read than the numpy code. This may be my bias, though --- I&#x27;ve been writing almost nothing but Cython for about two years now.<p>Btw, if anyone&#x27;s interested, &quot;cymem&quot; is a small library I have on pip. It&#x27;s used to tie memory to a Python object&#x27;s lifetime. All it does is remember what addresses it gave out, and when your Pool is garbage collected, it frees the memory.<p>Edit: GH fork, with code to compile and run the Cython version: <a href="https://github.com/syllog1sm/python-numpy-c-extension-examples" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;syllog1sm&#x2F;python-numpy-c-extension-exampl...</a> . I hacked his script quickly.
评论 #8483351 未加载
评论 #8483748 未加载
krastanov超过 10 年前
If I understand correctly, the `pypy` people strongly encourage the use of `cffi` instead of the CPython API, as the latter is tied too much to CPython and does not permit efficient JITing.
评论 #8483757 未加载
评论 #8483397 未加载
评论 #8484395 未加载
neotrinity超过 10 年前
I recently cythonized the performance critical parts of a numpy&#x2F;scipy based project with much success.<p>One does not necessarily need to get you hands dirty writing c-extensions ( although it can be a good exercise to learn the CPython API ).<p>In cython, you just need to sprinkle some static types to the inner loops and bump the speed up.
makmanalp超过 10 年前
There are also other approaches like the HOPE jit <a href="https://github.com/cosmo-ethz/hope" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cosmo-ethz&#x2F;hope</a> and Theano which is more about expression optimization and compilation: <a href="http://deeplearning.net/software/theano/" rel="nofollow">http:&#x2F;&#x2F;deeplearning.net&#x2F;software&#x2F;theano&#x2F;</a>
评论 #8483308 未加载
pdknsk超过 10 年前
Have you tried using ctypes? In particular ctypes.from_buffer is such a useful function.<p><a href="https://docs.python.org/2/library/ctypes.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;library&#x2F;ctypes.html</a><p>Unfortunately it doesn&#x27;t work in PyPy.<p><a href="https://bitbucket.org/pypy/pypy/issue/869/" rel="nofollow">https:&#x2F;&#x2F;bitbucket.org&#x2F;pypy&#x2F;pypy&#x2F;issue&#x2F;869&#x2F;</a>
JohnnyLee超过 10 年前
Part 2 of this series is posted here:<p><a href="https://www.crumpington.com/blog/2014/10-21-high-performance-python-extensions-part-2.html" rel="nofollow">https:&#x2F;&#x2F;www.crumpington.com&#x2F;blog&#x2F;2014&#x2F;10-21-high-performance...</a>.<p>Hacker News thread:<p><a href="https://news.ycombinator.com/item?id=8493366" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8493366</a>
syllogism超过 10 年前
Why not Cython?
评论 #8484246 未加载
评论 #8483018 未加载