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

前端 未结 4 1465
野性不改
野性不改 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:02

    When you write a function in python, a new function object is created, the function code is parsed and bytecompiled[and saved in the "func_code" attribute], so when you call that function the interpreter reads its bytecode and executes it.

    If you write the same function in C, following C/Python API to make it avaiable in python, the interpreter will create the function object, but this function won't have a bytecode. When the interpreter finds a call to that function it calls the real C function, thus it executes at "machine" speed and not at "python-machine" speed.

    You can verify this checking functions written in C:

    >>> map.func_code
    Traceback (most recent call last):
      File "", line 1, in 
    AttributeError: 'builtin_function_or_method' object has no attribute 'func_code'
    >>> def mymap():pass
    ... 
    >>> mymap.func_code
    ", line 1>
    

    To understand how you can write C code for python use follow the guides in the official site.

    Anyway, if you are simply doing N-dimensional array calculations numpy ought to be sufficient.

提交回复
热议问题