Python is slow, these are common reasons: "Because it is a GIL (Global Interpreter Lock)", "Because it is an interpreted language not a compiled language", "Because it is a dynamically typed language".
Recommended course:Java Tutorial.
Which reason has the greatest impact on performance?
"Because it's the GIL"
Modern computer CPUs have multiple cores and sometimes even multiple processors. In order to take advantage of all computing power, the operating system defines an underlying structure called threads, and a process (such as the Chrome browser) can generate multiple threads to execute system instructions through threads. In this way, if a process uses a lot of CPUs, the computing load will be shared among multiple cores, ultimately allowing most applications to complete tasks faster.
As of this writing, my Chrome browser has 44 threads open. In addition, the thread structure and API of POSIX-based operating systems (such as Mac OS and Linux) are different from those of Windows operating systems. The operating system is also responsible for thread scheduling.
If you have not written a multi-threaded program, then you should understand the concept of locks. Unlike a single-threaded process, in multi-threaded programming, you want to ensure that when changing variables in memory, multiple threads do not try to modify or access the same memory address at the same time.
CPython allocates memory when creating a variable, and then uses a counter to count the number of references to the variable. This concept is called "reference counting". If the number of references is 0, the variable can be released from the system. This way, creating "temporary" variables (such as in the context of a for loop) does not use up your application's memory.
The ensuing problem is that if the variable is shared among multiple threads, CPython needs to lock the reference counter. There is a "global interpreter lock" that carefully controls thread execution. No matter how many threads there are, the interpreter can only perform one operation at a time.
What impact does this have on the performance of Python applications?
If the application is single-threaded, single-interpreter, then this will not have any impact on speed. Removing the GIL will not affect the performance of the code.
But if you want to use an interpreter (a Python process) to achieve concurrency through threads, and the threads are IO-intensive (that is, there are a lot of network input and output or disk input and output), then the following will occur GIL Competition:
If a web application (such as Django) uses WSGI, then each request sent to the web application will be executed by a separate Python interpreter. Therefore there will only be one lock per request. Since the Python interpreter starts very slowly, some WSGI implementations support "daemon mode" to keep the Python process running for a long time.
"Because it's an interpreted language"
I've heard this reason a lot, and I find it oversimplifies how CPython actually works. When you write python myscript.py in the terminal, CPython initiates a long chain of operations, including reading, lexical analysis, syntax analysis, compilation, interpretation, and execution. The key point of this process is that it will generate a .pyc file during the compilation phase, and the bytecode will be written to a file under __pycache__/ (if it is Python 3), or written to the same directory as the source code (Python 2). This is true not only for scripts you write, but also for all code you import, including third-party modules.
So in most cases (unless the code you write will only run once), Python is interpreting the bytecode and executing it locally. Compare it to Java and C#.NET:
Java compiles source code into an "intermediate language", and then the Java virtual machine reads the bytecode and compiles it into machine code on the fly. The same goes for .NET CIL. .NET's common language runtime (CLR) uses just-in-time compilation to compile bytecode into machine code.
So, since they both use a virtual machine, and some kind of bytecode, why is Python so much slower than Java and C# in performance tests? The first reason is that .NET and Java are compiled just in time (JIT).
Just-in-time compilation, or JIT (Just-in-time), requires an intermediate language to divide the code into small pieces (or frames). Ahead of Time (AOT) means that the compiler translates the source code into code that the CPU can understand before executing it.
JIT itself does not make execution faster because it executes the same bytecode sequence. However, JIT can make optimizations at runtime. A good GIT optimizer can find the most executed parts of your application, called "hot spots." Those bytecodes are then optimized and replaced with more efficient code.
This means that if your application does something repeatedly, it will be much faster. Also, don't forget that both Java and C# are strongly typed languages, so the optimizer can make more assumptions about the code.
"Because it is a dynamically typed language"
"Static type" languages require that the type of a variable must be specified when it is defined, such as C, C, Java, C# and Go, etc.
Although there is also the concept of type in dynamically typed languages, the type of variables is dynamic.
a = 1 a = "foo"
In this example, Python defines a second variable with the same name and str type, and at the same time releases the memory occupied by the first instance of a.
The design purpose of statically typed languages is not to torture people, it is designed this way because this is how the CPU works. If any operation is ultimately converted into a simple binary operation, both objects and types need to be converted into low-level data structures.
Python has done all this for you, but you have never cared about it, and you don't need to care.
The lack of need to define types is not the reason why Python is slow. Python's design allows you to make everything dynamic. You can replace object methods at runtime, and you can patch underlying system calls at runtime. Almost anything is possible.
This design makes it difficult to optimize Python.
So, does Python’s dynamic typing make Python slower?
Comparing and converting types is expensive. Dynamically typed languages that check type
every time they are read, written, or referenced are difficult to optimize. The reason many alternatives to Python are so fast is that they sacrifice convenience for performance.
For example, Cython (http://cython.org/)combines C's static type with Python's method to make the types in the code known, thereby optimizing the code and able to Obtain 84 times performance improvement
Summary
The main reason why Python is slow is because of its dynamics and diversity. It can be used to solve a variety of problems, but most problems have better optimized and faster solutions.
But Python applications also have many optimization measures, such as using asynchronous, understanding performance testing tools, and using multiple interpreters.
For applications where startup time is not important and the code may enjoy the benefits of JIT, you may consider using PyPy.
For parts of the code where performance is important, if the variables are mostly static types, consider using Cython.
The above is the detailed content of Why is java faster than python?. For more information, please follow other related articles on the PHP Chinese website!