|
Original |
Translation |
|
82
|
Can Python be compiled to machine code, C or some other language?
|
|
|
83
|
Not easily. Python's high level data types, dynamic typing of objects and run-time invocation of the interpreter (using :func:`eval` or :func:`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``.
|
|
|
84
|
Several projects described in the Python newsgroup or at past `Python conferences <http://python.org/community/workshops/>`_ 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 <http://python.org/workshops/1997-10/proceedings/>`_ for more information.)
|
|
|
85
|
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.
|
|
|
86
|
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.
|
|
|
87
|
|
88
|
There are also several programs which make it easier to intermingle Python and C code in various ways to increase performance. See, for example, `Psyco <http://psyco.sourceforge.net/>`_, `Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_, `PyInline <http://pyinline.sourceforge.net/>`_, `Py2Cmod <http://sourceforge.net/projects/py2cmod/>`_, and `Weave <http://www.scipy.org/Weave>`_.
|
|
|
89
|
How does Python manage memory?
|
|
|
90
|
The details of Python memory management depend on the implementation. The standard C implementation of Python uses reference counting to detect inaccessible objects, and another mechanism to collect reference cycles, periodically executing a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. The :mod:`gc` module provides functions to perform a garbage collection, obtain debugging statistics, and tune the collector's parameters.
|
|
|
91
|
Jython relies on the Java runtime so the JVM's garbage collector is used. This difference can cause some subtle porting problems if your Python code depends on the behavior of the reference counting implementation.
|
|