我想弄清楚1984年3月16日有什么特别之处.在我使用的虚拟机上(没有什么特别之处),Python(以及PyPy)在尝试使用mktime时崩溃了,这似乎是一个非常合理的时间结构.
$ pypy
Python 2.7.3 (f66246c46ca30b26a5c73e4cc95dd6235c966b8f, Jul 30 2013, 09:27:06)
[PyPy 2.0.2 with GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import time
>>>> time.mktime((1984,3,16,0,0,0,0,0,0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range
>>>> time.mktime((1984,3,15,0,0,0,0,0,0))
448156800.0
>>>> time.mktime((1984,3,17,0,0,0,0,0,0))
448326000.0
>>>> time.mktime((1984,3,16,0,0,0,0,0,0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range
>>>> …
Run Code Online (Sandbox Code Playgroud) 我一直在测试我制作的缓存系统.其目的是加速Django Web应用程序.它将所有内容存储在内存中.根据cProfile,在我的测试中大部分时间都花在QuerySet._clone()中,结果证明效率非常低(实际上并没有那么奇怪).
我非常希望使用PyPy来加快速度.我有一台64位机器.然而,在安装了所有必需的库之后,事实证明PyPy编译的代码运行速度比常规Python代码慢2.5倍,我不知道该怎么做.代码是CPU绑定的(绝对没有数据库查询,因此IO边界不是一个选项).单个测试运行大约10秒,所以我想JIT应该足够了.我正在使用PyPy 1.5.一个注意事项 - 我自己没有编译源代码,只下载了一个64位的linux版本.
我想知道CPU密集型代码在PyPy下实际运行速度有多快.希望我能做些什么错误会阻止PyPy运行到最佳状态.
编辑
精确的cPython输出:
PyPy 1.5:
3439146 function calls (3218654 primitive calls) in 19.094 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
2/1 0.000 0.000 18.956 18.956 <string>:1(<module>)
2/1 0.000 0.000 18.956 18.956 /path/to/my/project/common/integrity/models/transactions.py:200(newfn)
2/1 0.000 0.000 18.956 18.956 /path/to/my/project/common/integrity/models/transactions.py:134(recur)
2/1 0.000 0.000 18.956 18.956 /usr/local/pypy/site-packages/django/db/transaction.py:210(inner)
2/1 0.172 0.086 18.899 18.899 /path/to/my/project/common/integrity/tests/optimization.py:369(func_cached)
9990 0.122 0.000 18.632 0.002 /usr/local/pypy/site-packages/django/db/models/manager.py:131(get)
9990 0.127 0.000 16.638 0.002 /path/to/my/project/common/integrity/models/cache.py:1068(get)
9990 0.073 0.000 12.478 0.001 /usr/local/pypy/site-packages/django/db/models/query.py:547(filter)
9990 0.263 …
Run Code Online (Sandbox Code Playgroud) 我试图在Windows 7 x64机器上使用PyPy,但没有找到任何方法来做到这一点.显然有一个win32二进制文件,但没有x64二进制文件或安装指南.我目前在win32上使用Python 2.7.2 win64(Python 2.7.2(默认,2011年6月12日,14:24:46)[MSC v.1500 64位(AMD64)].
从源代码安装引发了以下错误:
[translation:ERROR] WindowsError:[错误193]%1不是有效的Win32应用程序
有没有人有指导/提示在win64上使用PyPy?或者它是不可能的?
我目前正在解决Project Euler上的问题,到目前为止,我已经提出了这个问题的代码.
from itertools import combinations
import time
def findanums(n):
l = []
for i in range(1, n + 1):
s = []
for j in range(1, i):
if i % j == 0:
s.append(j)
if sum(s) > i:
l.append(i)
return l
start = time.time() #start time
limit = 28123
anums = findanums(limit + 1) #abundant numbers (1..limit)
print "done finding abundants", time.time() - start
pairs = combinations(anums, 2)
print "done finding combinations", time.time() - start
sums = map(lambda x: …
Run Code Online (Sandbox Code Playgroud) 我正在使用python多处理功能来映射某些元素的某些功能.有点像这样:
def computeStuff(arguments, globalData, concurrent=True):
pool = multiprocessing.Pool(initializer=initWorker, initargs=(globalData,))
results = pool.map(workerFunction, list(enumerate(arguments)))
return results
def initWorker(globalData):
workerFunction.globalData = globalData
def workerFunction((index, argument)):
... # computation here
Run Code Online (Sandbox Code Playgroud)
通常我使用cPython和Pypy在ipython中运行测试.我注意到产生的过程通常不会被杀死,所以它们开始积累,每个都使用一个ram.在计算过程中按ctrl-k会发生这种情况,这会将多处理发送到混乱的大狂热中.但即使让计算完成,这些过程也不会在Pypy中死亡.
根据文档,当池被垃圾收集时,它应该调用terminate()
并终止所有进程.这里发生了什么事?我必须明确打电话close()
吗?如果是,是否有某种上下文管理器可以正确管理关闭资源(即进程)?
这是在Mac OS X Yosemite上.
我已经读过(这里)PyPy通过cpyext支持CPython扩展模块.
我还没有找到任何cpyext文档.有没有?
我该如何使用它?
从源代码(例如这里),我发现要加载我的leveldb.so
模块,我可能必须这样做:
import cpyext
cpyext.load_module("leveldb.so","leveldb")
Run Code Online (Sandbox Code Playgroud)
但是,这会因此错误而崩溃:
Fatal Python error: PyThreadState_Get: no current thread
Run Code Online (Sandbox Code Playgroud)
我在回溯中注意到它从我的CPython调用函数,而不是从PyPy调用:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff8b3e4d46 __kill + 10
1 libsystem_c.dylib 0x00007fff927a9df0 abort + 177
2 org.python.python 0x0000000104692eaa Py_FatalError + 49
3 org.python.python 0x0000000104691370 PyThreadState_Get + 28
4 org.python.python 0x000000010468cf16 Py_InitModule4_64 + 58
5 leveldb.so 0x00000001027e0881 initleveldb + 49 (leveldb_ext.cc:59)
6 pypy 0x0000000100f59bb3 PyLong_CheckExact + 55379
7 pypy 0x0000000100f6e7c7 PyLong_CheckExact + 140391
....
Run Code Online (Sandbox Code Playgroud) 我刚刚在Windows上安装了PyPy,并且在我运行的一些模拟代码中看到了大约10倍的速度提升.我也希望看到使用numpy的代码类似.我不是一个经验丰富的Python程序员,但我发现很难遵循这些说明.有没有人知道如果在Windows上为PyPy安装numpy是可能的,如果是这样,最简单的方法是什么?
说明提供了两个选项.
选项1
如果你有pip(命令行假定它找到属于PyPy的pip,而不是CPython中的pip):
pip install git+https://bitbucket.org/pypy/numpy.git
Run Code Online (Sandbox Code Playgroud)
这似乎是一个很好的选择,但我pip
在目录结构中找不到我解压缩.
选项2
或者,直接的方式:
git clone https://bitbucket.org/pypy/numpy.git
cd numpy
pypy setup.py install
Run Code Online (Sandbox Code Playgroud)
我没有登录git
Windows,但我也怀疑这个选项可能涉及从Windows编译源代码https://bitbucket.org/pypy/numpy.git
,甚至可能无法(或需要大量黑客攻击).
编辑
通过安装PIP https://sites.google.com/site/pydatalog/python/pip-for-windows按照下面的答案,或在说明这个答案的问题,我如何在Windows上安装PIP?,失败了ConnectionError: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /packages/py2.py3/p/pip/pip-1.5.4-py2.py3-none-any.whl (Caused by <class 'httplib.BadStatusLine'>: '')
.例如,参见此错误报告.不过,我是能够通过微软的Visual Studio安装PIP PTVS通过右键单击在Solution Explorer中的Python环境PyPy 2.7,选择安装Python包.这无法安装numpy(另一个连接错误),但确实安装了pip.
现在我有了pip,我尝试使用命令行安装numpy pip install git+https://bitbucket.org/pypy/numpy.git
.首先我需要安装git才能做到这一点......没问题.但随后它失败了以下
building library "npymath" sources
No module named numpy.distutils.msvccompiler in numpy.distutils; …
Run Code Online (Sandbox Code Playgroud) PyPy 的主页上说 Guido van Rossum 说:
如果你想让你的代码运行得更快,你可能应该只使用 PyPy。
PyPy 的主页并未提及此声明是在何处或在什么上下文中做出的。他有没有在会议上说,与某人面对面交谈?无论如何,周围的话题是什么?是关于 Python 的性能吗?
今天我input()
通过任何算法问题知道pypy3在时间上比python3快。性能差异几乎高达 12 倍。
为什么会有这样的差异?
pypy ×10
python ×7
jit ×2
compilation ×1
cprofile ×1
cpython ×1
luajit ×1
memory ×1
mktime ×1
numpy ×1
optimization ×1
python-3.x ×1
timezone ×1
win64 ×1
windows ×1