Python代码怎样动起来?(还是自己译的)

Today, Python is "interpreted" in the same way Java is: Python source code is automatically compiled (translated) to an intermediate and platform-neutral form called bytecode, which is then executed by the Python virtual machine (that is, the Python runtime system). Translation to bytecode happens when a module is first imported, and it is avoided when possible to speed program startup: bytecode is automatically saved in .pyc files and, unless you change the corresponding source file, loaded directly the next time your program runs.

今天,Python就跟Java一样,源代码自动编译(或者可以说“翻译”)至中间字节码,然后由Python虚拟机执行(nnd,又是个鸟东西)。一旦一个模块第一次引入时,就翻译成字节码,为了避免减慢程序的启动速度,字节码放在.pvc文件中,除非改动了对应的源文件,一般来说下次可以直接使用。

This bytecode compilation model makes Python scripts portable and faster than a pure interpreter that runs raw source code lines. But it also makes Python slower than true compilers that translate source code to binary machine code. Bytecode is not machine code and is ultimately run by the Python (or other) virtual machine program, not directly by your computer's hardware.

这种字节码编译模型使得Python比其它纯粹解释型的脚本语言更加容易迁移平台,也更快(废话),但也正因为如此,它比那些将源代码编译成二进制机器码的语言又来得慢(更是废话)。因为字节码不由硬件执行,而由Python虚拟机在run

Keep in mind, though, that some of these details are specific to the standard Python implementation. For instance, the Jython system compiles Python scripts to Java bytecode, and the IronPython implementation compiles Python source code to the bytecode used by the C#/.NET environment. In addition, Python compiler-related projects have been spawned in the past and will likely continue into the future. For more details on this front, see the following:

然而请记住:针对标准的Python实现,不同port有不同的实现。例如,Jython将Python编译成Java虚拟机字节码(nnd,慢上加慢),IronPython实现编译成C#使用的IL代码(嗯,看起来比较帅)。当然了,已经写就的Python程序都已经生根发芽了,也就无所谓了。

  • The Psyco just-in-time compiler for Python, which replaces portions of a running program's bytecode with optimized binary machine code tailored to specific datatypes. Psyco can speed Python programs by any factor from 2 to 100. The high end is more likely for heavily algorithmic code, whereas I/O-bound programs don't improve as much. (In my own experience, a 3x-5x speedup is common for typical programsamazing for a simple install.)

  • Psyco JIT编译器,可以将Python字节码优化成机器码,所以能提高速度2~100倍Surprise

  • A related project, PyPy, which aims to reimplement the Python virtual machine to better support optimizations. The PyPy project may incorporate and subsume Psyco's techniques.

  • 嗯嗯,好东西当然谁都想要,PyPy,同样用来优化Python。可能会对Psyco取而代之

  • The Parrot project, which seeks to develop a bytecode and virtual machine that will be shared by many languages, including Python.

  • The Installer, Py2Exe, and Freeze systems, which package Python programs as standalone executables known as "frozen binaries"a combination of your bytecode and the Python virtual machine. Frozen binaries do not require that Python be installed on the receiving end.

  • 用来打包Python程序的东东,Py2Exe,会将Python打包成叫做“冰冻二进制码”Laughing这样,在客户端就不需要Python环境了

  • Other program distribution formats, including zip archives (with modules automatically extracted on imports); Python eggs (an emerging package format); Distutils (an installation script system); and encrypted bytecode (for instance, using PyCrypto and the import hooks).

  • 另外还有一些工具,包括zip压缩包,Python鸡蛋?(一种合并格式),Distutils-一种脚本安装系统

  • The emerging Shed Skin system, which translates Python source code to C++. This system assumes that your code will not use the full range of Python's dynamic typing, but this constraint allows highly efficient code to be generated, which is by some accounts faster than Psyco and much faster than standard Python. Shed Skin's own website reports speedups of 12 and 45 times faster on average than Psyco and standard CPython, respectively, though results can vary greatly.

Psyco may provide a simpler optimization path for some programs than linked-in C libraries, especially for algorithm-intensive code. Although Python's extreme dynamic nature makes compilation complex (the behavior of "x + 1" cannot be easily predicted until runtime), a future optimizing Python compiler might also make many of the performance notes in this chapter moot points.

最后是一堆废话,用来提高Python执行速度的Psyco...效率提高的同时,程序就不好看了

你可能感兴趣的:(python)