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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Speeding up non-vectorizable code with Cython

37 点作者 callmekit将近 10 年前

3 条评论

Veedrac将近 10 年前
Before jumping to Cython, remove things like exponentiation and all the calls to `ord`:<p><pre><code> def column_to_index(col): col_index = 0 for byte in bytearray(col): col_index *= 26 col_index += byte - 64 return col_index - 1 </code></pre> FWIW, you _can_ vectorize this, albeit it&#x27;s neither simple nor fast, due to Numpy&#x27;s fixed overheads. If you convert thousands of strings at once, though, it&#x27;s probably reasonably speedy.<p><pre><code> import numpy from numpy import arange place_values = 26 ** arange(20, dtype=object)[::-1] overhead = numpy.add.accumulate(64 * place_values[::-1]) + 1 def column_to_index(col): digits = bytearray(col) length = len(digits) summation = place_values[-length:].dot(digits) return summation - overhead[length-1]</code></pre>
Russell91将近 10 年前
Nice article. Shameless plug: readers that would like to use cython outside of the convenient ipython notebook interface should look into runcython [1]. You can run any python file using cython with:<p><pre><code> sudo pip install runcython mv main.py main.pyx &amp;&amp; runcython main.pyx </code></pre> [1] <a href="http:&#x2F;&#x2F;github.com&#x2F;russell91&#x2F;runcython" rel="nofollow">http:&#x2F;&#x2F;github.com&#x2F;russell91&#x2F;runcython</a>
lorenzhs将近 10 年前
The type limitations example seems a bit contrived, a spreadsheet with 2^64 is slightly unrealistic. The point remains, though.