python文档指出,cPickle比Pickle快的原因是前者是用C实现的。这到底是什么意思?
我正在用 Python 制作一个高级数学模块,一些计算需要大量时间。这是否意味着如果我的程序是用 C 语言实现的,它可以做得更快吗?
我希望从其他 Python 程序中导入这个模块,就像我可以导入 cPickle 一样。
你能解释一下如何在 C 中实现一个 Python 模块吗?
python文档指出,cPickle比Pickle快的原因是前者是用C实现的。这到底是什么意思?
我正在用 Python 制作一个高级数学模块,一些计算需要大量时间。这是否意味着如果我的程序是用 C 语言实现的,它可以做得更快吗?
我希望从其他 Python 程序中导入这个模块,就像我可以导入 cPickle 一样。
你能解释一下如何在 C 中实现一个 Python 模块吗?
您可以编写快速的 C 代码,然后在您的 python 脚本中使用它,这样您的程序将运行得更快。[1] http://docs.python.org/extending/index.html#extending-index
一个例子是 Numpy,用 C ( https://numpy.org/ )编写
典型的用途是在 C 中实现瓶颈(或者使用用 C 编写的库,当然;)),由于它的速度,其余代码使用 python
[1] 顺便说一下,这就是 cPickle 比 pickle 快的原因
编辑:
看看 Pyrex: http: //www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/About.html
'Pyrex 是一种专门为编写 Python 扩展模块而设计的语言。它旨在弥合 Python 的漂亮、高级、易于使用的世界和杂乱的低级 C 世界之间的鸿沟。
这不是“官方”方式,但它可能有用
As mentioned, numpy is excellent for vector computations. (Could be better still, but the comment that it's better than anything you could write without actually doing work is definitely true.)
Not everything can be easily vectorized, though, so if you do have tight inner loops with lots of function calls (say a heavily recursive algorithm) you still have a couple of options: probably the most popular is Cython, which allows you to write modules and functions in a kind of annotated Python and get C-like speed when you need it.
Or maybe your time is all dominated by library calls to compute eigenvalues or invert matrices or evaluate special functions or divide really large integers -- many of which the Sage project handles very well, by the way, if what you're doing is more mathematical than pure crunching -- in which case the time spent in Python might not even matter. It all depends on the details of the kind of numerics you're doing.
当您在 python 中编写函数时,会创建一个新的函数对象,函数代码被解析和字节编译[并保存在“func_code”属性中],因此当您调用该函数时,解释器读取其字节码并执行它。
如果您在 C 中编写相同的函数,遵循 C/Python API 使其在 python 中可用,解释器将创建函数对象,但该函数没有字节码。当解释器找到对该函数的调用时,它会调用真正的 C 函数,因此它以“机器”速度而不是“python 机器”速度执行。
您可以验证这个用 C 编写的检查函数:
>>> map.func_code
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'func_code'
>>> def mymap():pass
...
>>> mymap.func_code
<code object mymap at 0xcfb5b0, file "<stdin>", line 1>
要了解如何使用 Python 编写 C 代码,请遵循官方网站中的指南。
无论如何,如果您只是简单地进行 N 维数组计算,numpy 应该就足够了。
除了已经提到的 Pyrex/Cython,您还有其他选择:
Shed Skin:将 Python 的(受限子集)翻译成 C++。可以自动为您生成扩展名。您将创建一个扩展来执行此操作(假设 Linux):
wget http://shedskin.googlecode.com/files/shedskin-0.7.tgz
tar -xzf shedskin-0.7.tgz
# On your code folder:
PYTHONPATH=/path/to/shedskin-0.7 python shedskin -e yourmodule.py
# The above generates a Makefile and a yourmodule.h/.cpp pair
make
# Now you can "import yourmodule" from Python and check it's from the .so by "print yourmodule.__file__
PyPy:更快的 Python,带有 JIT 编译器。你可以简单地在它上面运行你的代码而不是 CPython。目前仅支持 Python 2.5,即将支持 2.7。可以极大地加速数学繁重的代码。要安装并运行它(假设 Linux 32 位):
wget http://pypy.org/download/pypy-1.4.1-linux.tar.bz2
tar -xjf pypy-1.4.1-linux.tar.bz2
sudo ln -s /path/to/pypy-1.4.1-linux/bin/pypy /usr/local/bin
# Then, instead of "python yourprogram.py" you'll just run "pypy yourprogram.py"
Weave:允许您编写 C inline,编译它。
编辑:如果您希望我们为您运行这些工具并进行基准测试,只需发布您的代码;)