- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是对 Project Euler Problem 49 的一次(有点乱)尝试.
我应该直截了本地说 deque
不是一个好的选择!我的想法是缩小素数集以测试成员资格会导致循环加速。然而,当我意识到我应该使用 set
(而不用担心删除元素)时,我得到了 60 倍的加速。
from collections import deque
from itertools import permutations
from .sieve import sieve_of_erastothenes # my own implementation of the Sieve of Erastothenes
primes = deque(prime for prime in sieve_of_erastothenes(10000) if prime > 1000 and prime != 1487) # all four-digit primes except 1487
try:
while True:
prime = primes.popleft() # decrease the length of primes each time to speed up membership test
for inc in xrange(1,10000 + 1 - (2 * prime)): # this limit ensures we don't end up with results > 10000
inc1 = prime + inc
inc2 = prime + 2*inc
if inc1 in primes and inc2 in primes:
primestr = str(prime)
perms = set(''.join(tup) for tup in permutations(primestr)) # because permutations() returns tuples
inc1str = str(inc1)
inc2str = str(inc2)
if inc1str in perms and inc2str in perms:
print primestr + inc1str + inc2str
raise IOError # I chose IOError because it's unlikely to be raised
# by anything else in the block. Exceptions are an easy
# way to break out of nested loops.
except IOError:
pass
无论如何,在我想到使用 set
之前,我在 Pypy 中尝试过。我发现结果相当令人惊讶:
$ time python "problem49-deque.py"
296962999629
real 1m3.429s
user 0m49.779s
sys 0m0.335s
$ time pypy-c "problem49-deque.py"
296962999629
real 5m52.736s
user 5m15.608s
sys 0m1.509s
为什么 Pypy 在这段代码上慢了五倍以上?我猜 Pypy 版本的 deque
是罪魁祸首(因为它在 set
版本上运行得更快),但我不知道为什么会这样。
最佳答案
慢的部分是inc1 in primes and inc2 in primes
。我会看看为什么 PyPy 这么慢(基本上感谢性能错误报告)。请注意,正如您提到的那样,代码可以变得非常快(在 PyPy 和 CPython 上)---在这种情况下,只需将 primes
双端队列复制到 for 之前的集合中
循环。
关于python - 为什么 Pypy 的双端队列这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13421326/
在PyPy的 build 阶段,该脚本会生成一些看起来非常有趣的漂亮 ASCII 字符。每个字符代表什么?如何解释这个输出?在生成这种 ASCII 艺术时,PyPy build 到底在做什么? 最佳答
如何使用 PyPy(最好是 pypy3)而不是带有 poetry 的标准 python? 也就是说,pyproject.toml中应该存储什么?是否还需要进行其他更改? 最佳答案 事实证明,它就像安装
将 pypy 实现转换为 c 文件并在配备 2G 内存和 Intel Core2 2GHz CPU 的现代笔记本上构建 pypy-c 需要花费数小时。 我知道这是一个 CPU 密集型任务,但它必须这么
每次在 PyPI 上发布 Virtualenv 的新版本时,我都希望收到通知,最好通过电子邮件或 RSS 提要。这可能吗? 最佳答案 是的,可以从 pypi.org 获得 RSS 提要,请参阅:htt
我有一个 Python 扩展,需要针对基于 Windows 的 Appveyor 持续集成服务上的 PyPy 解释器进行编译。我如何在那种环境中获得 PyPy? 最佳答案 将此 PowerShell
PyPy GIL 是 RPython 中 PyPy 解释器实现的一部分,还是 translate.py 自动添加的东西?即,如果我要在 RPython 中编写自己的新语言解释器并通过 translat
我已阅读 PyPy -- How can it possibly beat CPython?以及无数其他事情,但我无法理解用 Python 编写的东西如何比 Python 本身更快。 我能想到的唯一方
我正在处理一些 OpenAI 的 API 集成。我在安装 tiktoken(用于 OpenAI 模型的快速 BPE 标记器)时遇到问题。 当我尝试在我的 macbook 机器上运行时: pip3 in
我正在处理一些 OpenAI 的 API 集成。我在安装 tiktoken(用于 OpenAI 模型的快速 BPE 标记器)时遇到问题。 当我尝试在我的 macbook 机器上运行时: pip3 in
这可能是一个非常愚蠢的问题,但是“PyPy”和“PyPI”之间有什么区别?它们是一回事吗? 最佳答案 PyPy是 python 的替代实现: PyPy is a fast, compliant alt
我想在与 Python 2.7 版本兼容的 window 7 上卸载 pypy 版本。控制面板的“卸载程序”中没有 pypy。到目前为止,我还没有找到任何文档。 最佳答案 PyPy 不会在 Windo
PyPy 是否在编译时进行静态类型检查以在编译时捕获类型错误?如果不是,像 HM 类型推断这样的东西是否有助于在编译时捕获这些错误? 最佳答案 否 在两个帐户上。 (我假设 PyPy 是指具有 JIT
我在弄清楚如何让我的 Python 包处理到 PyPi 时遇到了一些问题,这样我就可以很容易地将它与其他项目的其他依赖项一起安装(即使用需求条目或简单的“pip install xyz”)-这里列出了
例如,如果我有一个 Pypi 上已经存在的包 foo。我希望能够在我的 setup.py 中使用新版本号进行 git Push origin master 操作,然后 Pypi 以某种方式自动更新其版
新版 PyPy 附带了集成的 Stackless。据我所知,捆绑的 Stackless 与 2001 年的原始 Stackless 不同。所以主要是带有调度器的绿色线程框架。 Greenlet 是 S
我按照指南 How to submit a package to PyPI提交一个包裹。它抛出了以下错误: Traceback (most recent call last): Fi
我使用诗歌创建了一个Python包然后我使用诗歌构建、诗歌发布将我的包发布到 PyPI,但 PyPI 中缺少我的包描述。 该项目托管在 GitHub 中:https://github.com/fsis
我听说过很多关于 PyPy 项目的信息。他们声称它比 CPython 上的 their site 解释器快 6.3 倍。 每当我们谈论 Python 等动态语言时,速度都是最重要的问题之一。为了解决这
我们将 artifactoy 更新到最新版本 5.10.3 以支持 pypi 更新。 在这次更新之后,我们遇到了一些非常慢的 pip install --upgrade 时间。pip install
我一直在 ESP32-WROOM-32D 上使用 uasyncio。在所有测试之后,我正在尝试将固件用于我的其他板,但默认情况下 uasyncio 并未与 micropython 一起安装。我尝试通过
我是一名优秀的程序员,十分优秀!