From: Benjamin Peterson Date: Wed, 6 Sep 2017 01:18:16 +0000 (-0700) Subject: [2.7] Issue GH-28705: greatly simplify the FAQ entry on transpiling. (#3371) X-Git-Tag: v2.7.15rc1~219 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6d6ff08d38bec7e3f844079c681bafad16e1d6e1;p=python [2.7] Issue GH-28705: greatly simplify the FAQ entry on transpiling. (#3371) This also eliminats a dead link to Weave in the process.. (cherry picked from commit 78ffd6cffacb04bea61bb0ef850d05859ab2dbe4) --- diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 0a8cfdd25d..15e4af57fe 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -391,50 +391,11 @@ is exactly the same type of object that a lambda expression yields) is assigned! Can Python be compiled to machine code, C or some other language? ----------------------------------------------------------------- -Not easily. Python's high level data types, dynamic typing of objects and -run-time invocation of the interpreter (using :func:`eval` or :keyword:`exec`) -together mean that a "compiled" Python program would probably consist mostly of -calls into the Python run-time system, even for seemingly simple operations like -``x+1``. - -Several projects described in the Python newsgroup or at past `Python -conferences `_ have shown that this -approach is feasible, although the speedups reached so far are only modest -(e.g. 2x). Jython uses the same strategy for compiling to Java bytecode. (Jim -Hugunin has demonstrated that in combination with whole-program analysis, -speedups of 1000x are feasible for small demo programs. See the proceedings -from the `1997 Python conference -`_ for more information.) - -Internally, Python source code is always translated into a bytecode -representation, and this bytecode is then executed by the Python virtual -machine. In order to avoid the overhead of repeatedly parsing and translating -modules that rarely change, this byte code is written into a file whose name -ends in ".pyc" whenever a module is parsed. When the corresponding .py file is -changed, it is parsed and translated again and the .pyc file is rewritten. - -There is no performance difference once the .pyc file has been loaded, as the -bytecode read from the .pyc file is exactly the same as the bytecode created by -direct translation. The only difference is that loading code from a .pyc file -is faster than parsing and translating a .py file, so the presence of -precompiled .pyc files improves the start-up time of Python scripts. If -desired, the Lib/compileall.py module can be used to create valid .pyc files for -a given set of modules. - -Note that the main script executed by Python, even if its filename ends in .py, -is not compiled to a .pyc file. It is compiled to bytecode, but the bytecode is -not saved to a file. Usually main scripts are quite short, so this doesn't cost -much speed. - -.. XXX check which of these projects are still alive - -There are also several programs which make it easier to intermingle Python and C -code in various ways to increase performance. See, for example, `Cython `_ , `Psyco -`_, `Pyrex -`_, `PyInline -`_, `Py2Cmod -`_, and -`Weave `_. +`Cython `_ compiles a modified version of Python with +optional annotations into C extensions. `Nuitka `_ is +an up-and-coming compiler of Python into C++ code, aiming to support the full +Python language. For compiling to Java you can consider +`VOC `_. How does Python manage memory?