Why not write a compiler for an interpreted language like Python?
Source: Internet
Author: User
As the question. The interpreter language has poor performance. Why can't a language have both compilers and interpreters? In this way, you can compile it when performance is required. I started to get involved in computer science, engineering students, light spray... As the question. The interpreter language has poor performance. Why can't a language have both compilers and interpreters? In this way, you can compile it when performance is required.
I started to get involved in computer science, engineering students, light spray... Reply: CPython is compiled into bytecode. For details, see the pyc file. Other JPython and IronPython are also compiled into specific bytecode. Pypy can also be further compiled into machine code by JIT.
The main performance issue is not caused by compilation or not, but caused by dynamic type systems and various additional types of actions. The subject is 2c-python-2C. py.
Is this static compiler?
Similar brain holes cannot be opened by only one person. See Nuitka.
The author is still very excited: Static Compilation-That is the point.
<-Check whether A static Python compiler? What's the point?
For more information, see PythonImplementations on the Python wiki.
I'm glad to tell you that python is not simply an explanatory language. The python interpreter we usually call is actually Cpython. During execution, python will. the py file is compiled into an intermediate bytecode and stored in the memory. Then, the bytecode is interpreted as a machine-identifiable binary code during actual execution.
By default, the byte code compiled by the imported file will be saved, that is, the. pyc file we see. Of course, we can compile a. py file and save it.
The static language compiles binary files. That is to say, after the compilation is completed, it is a good idea for the machine to run the files.
Python is a dynamic language, such as the statement a + B. Before executing it, you have no idea what a and B are. Is it an integer operation? Or floating point calculation? You must know that in general computers, the number of operation units for integer and floating-point operations is different.
So how do you fully compile dynamic languages? Python has done a good job.
If you are a pure python, that is, there are not many third-party libraries, you can consider using the pypy interpreter. However, pypy does not support most third-party libraries. For example, the powerful scientific computing database numpy is not supported. Of course, the supported days are not too long.
Moreover, most of the speed bottlenecks have little to do with the language, but with algorithms. This is really not the case. Considering using C/C ++ or CUDA acceleration is the king.
The first time I coded so many words, it was really tiring ....
Update:
@ Kalam yum mentioned that numpy has a special Pypy version, and the official website does provide a download link. However, the support is not strong enough. I want to try it out and find that it is not enough. It seems to be the reason for my poor network. For the moment, I will not consider using PyPy.
Some people mentioned the combination of CPython and PyPy on the Internet:
Https://github.com/fijal/jitpy
You can check it out.
At present, it still seems that sentence:
Using C/C ++ or CUDA acceleration is king.
Using C/C ++ or CUDA acceleration is king.
Using C/C ++ or CUDA acceleration is king.
How can I learn C/C ++ and CUDA while learning Python? Because python is a dynamic language. Many features depend on program metadata. Therefore, even if the code is compiled into a machine code, the runtime, garbage collector, and metadata of the program must be carried. Compilation into machine code may improve the performance of numerical computation, but other aspects may not be significantly improved.
Compile the code as a machine code, which is similar to the jit of pypy, but only saves the compilation result. In fact, most of the scenarios that require scientific computing to use python do not care about its performance. (Shake your head
Cython: C-Extensions for Python
You mean this? Python is not simply an interpreted language, so it can be considered as a so-called interpreter, that is, a universal compiler. This problem is like why a horse is not as comfortable as a person is lying down and sleeping.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.