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 快的原因
编辑:
看看耐热玻璃:http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/About.html
'Pyrex is a language specially designed for writing Python extension modules. It's designed to bridge the gap between the nice, high-level, easy-to-use world of Python and the messy, low-level world of C. '
这不是“官方”方式,但可能有用
关于python - 在 Python 中,为什么用 C 实现的模块比纯 Python 模块更快,我该如何编写一个模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4616129/