In Python, why is a module implemented in C faster than a pure Python module, and how do I write one?

前端 未结 4 1363
野性不改
野性不改 2021-02-14 09:54

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?

I am making a module

4条回答
  •  天涯浪人
    2021-02-14 10:18

    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

    An example is Numpy, written in C ( https://numpy.org/ )

    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] by the way, this is why cPickle is faster than pickle

    edit:

    take a look at Pyrex: 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. '

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

提交回复
热议问题