What makes Python computing so inefficient?
Python is a very popular programming language, because of its simple syntax, powerful, easy to start, has been widely used in artificial intelligence and other fields, but python computing efficiency is low, what is the specific reason for it, the following list some: First: python is a dynamic language
The type of the object pointed to by a variable is determined at runtime, the compiler can't make any predictions and there's no way to optimize it. As a simple example: r = a + b. a and b are added, but the types of a and b are not known until runtime. For addition operations, different types are handled differently, so each runtime goes through and determines the types of a and b, and then performs the corresponding operations. And in static languages such as C++, the runtime code is determined at compile time.
Another example is attribute lookup, and the specific lookup order is described in detail in "python attribute lookup". In short, accessing a property of an object is a very complex process, and the python objects accessed through the same variable may all be different (see Lazy property for an example). And in C, accessing the property is done with the address of the object plus the offset of the property.
Second: python is interpreted execution, but does not support JIT (just in time compiler). Although the big-name google once tried Unladen Swallow this project, but eventually also folded.
Third: everything in python is an object, and each object needs to maintain a reference count, adding extra work.
Fourth: python GIL, GIL is one of the most criticized points of Python, because of GIL, multithreading in python is not really concurrent. If it is in an IO bound business scenario, this is not a big problem, but in a CPU BOUND scenario, this is fatal. So I don't use python multithreading much in my work, usually using multiple processes (pre fork), or in addition to concurrent threads. Even in single thread, GIL has a significant performance impact, as python tries to switch threads every 100 OPCode executions (the default, which can be set via sys.setcheckinterval()), as specified in the source code in ceval.c::PyEval_EvalFrameEx.
Fifth: Garbage collection, this one is probably common to all programming languages with garbage collection. python uses a mark-and-generate garbage collection strategy, which interrupts the executing program each time it is garbage collected, causing what is called a stutter. There's an article on infoq that mentions a 10% performance boost for Instagram after disabling Python's GC mechanism. Interested readers can go for a closer grind.