在Python,为什么在C比纯Python模块来实现更快的一个模块,我该怎么写呢? [英] In Python, why is a module implemented in C faster than a pure Python module, and how do I write one?

查看:142
本文介绍了在Python,为什么在C比纯Python模块来实现更快的一个模块,我该怎么写呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python文档的状态,即cPickle的原因是快于味酸是,前者是用C实现的是什么意思是什么呢?

The python documentation states, that the reason cPickle is faster than Pickle is, that the former is implemented in C. What does that mean exactly?

我想提出一个模块在Python先进的数学和一些计算需要很长时间显著量。这是否意味着,如果我的程序是用C语言实现它可以制成更快?

I am making a module for advanced mathematics in Python, and some calculations take a significant amount of time. Does that mean that if my program is implemented in C it can be made much faster?

我想从其他Python程序中导入这个模块,只是我可以导入cPickle的方式。

I wish to import this module from other Python programs, just the way I can import cPickle.

你能解释一下怎么办落实在C Python模块?

Can you explain how to do implement a Python module in C?

推荐答案

您可以编写快速的C code,然后用它在你的Python脚本,让你的程序将运行得更快。 [1]
http://docs.python.org/extending/index.html#extending-index

you can write fast C code and then use it in your python scripts, so your program will run faster. [1] http://docs.python.org/extending/index.html#extending-index

一个例子是numpy的,写在C( http://numpy.scipy.org/

an example is Numpy, written in C ( http://numpy.scipy.org/ )

一个典型的应用是落实瓶颈C(或用C语言编写的一个图书馆,当然;)),由于其速度,使用Python为剩余code

a typical use is to implement the bottleneck in C (or to use a library written in C, of course ;) ), due to its speed, and to use python for the remaining code

[1]的方式,这就是为什么cPickle的比咸菜快

[1] by the way, this is why cPickle is faster than pickle

编辑:

看一看耐热玻璃:的http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/About.html

耐热玻璃是专门的语言
  专为编写Python扩展
  模块。它旨在弥合
  漂亮的,高层次的差距,
  易于使用世界Python和的
  凌乱,C的低级别的世界

'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. '

这不是官方的方式,但它可能是有用的。

it's not the 'official' way but it may be useful

这篇关于在Python,为什么在C比纯Python模块来实现更快的一个模块,我该怎么写呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆