Home > Article > Backend Development > why python is slow
Python is a dynamically typed, interpreted language. For many developers, it is well known that Python runs slowly. Its characteristic that everything is an object is one of the reasons for its slow running. Below are three reasons: Let’s analyze the reasons why Python is slow from various aspects.
Dynamic typing
Python is dynamically typed rather than statically typed, which means that when the program is executed , the interpreter does not know the type of the variable. For C language, the compiler knows the type of a variable when it is declared; for Python, it only knows that a variable is some kind of Python object when the program is executed.
For the following C code
int a = 1; int b = 2; int c = a + b;
The compiler always knows that a and b are integers. When performing the addition operation, the process is as follows:
Put bd43222e33876353aff11e13a7dc75f6 1 Assign value to a
Assign bd43222e33876353aff11e13a7dc75f6 2 Assign value to b
Call binary_add4101e4c0559e7df80e541d0df514fd80(a, b)
Assign the result to c
The Python code to achieve the same function is as follows:
a = 1 b = 2 c = a + b
Related recommendations: "Python Video Tutorial"
The interpreter only knows that 1 and 2 are objects, but does not know The type of this object. Therefore, the interpreter must check the PyObject_HEAD of each variable to know the variable type, then perform the corresponding addition operation, and finally create a new Python object to save the return value. The general process is as follows:
The first step, Assign 1 to a
1. Set a->PyObject_HEAD->typecode to integer
2. Set a->val = 1
The second step , assign 2 to b
1. Set a->PyObject_HEAD->typecode to integer
2. Set b->val = 2
The third step, call binary_add4101e4c0559e7df80e541d0df514fd80(a, b)
1.a->PyObject_HEAD to get the type encoding
2.a is an integer; the value is a ->val
3.b->PyObject_HEAD gets the type encoding
4.b is an integer, the value is b->val
5. Call binary_add4101e4c0559e7df80e541d0df514fd80(a->val, b->val)
6. The result is an integer and is stored in result
The fourth step is to create the object c
1.Set c->PyObject_HEAD->typecode to integer
2.Set c->val to result
Dynamic type means that any operation will involve more A step of. This is the main reason why Python's numerical operations are slower than C language
Python is an interpreted language
The above introduces a difference between interpreted code and compiled code. A smart compiler can anticipate and optimize repeated or unnecessary operations in advance, which can lead to performance improvements. Compilers are a big topic and will not be discussed here.
Python's object model will bring inefficient memory access
When compared with the integers of C language, we pointed out that Python has an extra layer of information. Now let’s look at the array situation. In Python we can use the List object provided in the standard library; in C language we will use a buffer-based array.
The simplest NumPy array is a Python object built around C data, which means it has a pointer to a contiguous data buffer. Python's list has pointers to continuous pointer buffers. Each of these pointers points to a Python object. Combined with the above example, these Python objects are an integer object. This structure looks like the following
It's easy to see that if you are performing operations that step through the data in order, numpy's memory layout is more efficient than Python's memory layout, both in terms of storage cost and access time cost .
The above is the detailed content of why python is slow. For more information, please follow other related articles on the PHP Chinese website!