由于python是开源的,这就是为什么我们可以根据我们的需求定制python。自定义之后,我们可以根据需要命名该版本。这就是为什么python有多种口味。每种口味都是python的定制版本,以满足特殊要求。这类似于UNIX有多种版本,比如Ubuntu、Linux、RedHat Linux等等。以下是python的一些风味:
CPython
Default implementation of python programming language which we download from python.org, provided by python software foundation. It is written in C and python. It does not allow us to write any C code, only python code are allowed. CPython can be called as both an interpreter and a compiler as here our python code first gets compiled to python bytecode then the bytecode gets interpreted into platform-specific operations by PVM or Python Virtual Machine. Keep in mind interpreters have language syntaxes predefined that's why it does not need to translate into low level machine code. Here Interpreter just executes bytecode on the fly during runtime and results in platform-specific operations.
Old versions of JavaScript, Ruby, Php were fully interpreted languages as their interpreters would directly translate each line source code into platform-specific operations, no bytecode was involved. Bytecode is there in Java, Python, C++ (.net), C# to decouple the language from execution environment, i.e. for portability, write once, run anywhere. Since 2008, Google Chrome's V8 JavaScript engine has come up with Just-In-Time Compiler for JavaScript. It executes JavaScript code line-by-line like an interpreter to reduce start up time, but if encounters with a hot section with repeatedly executed line of code then optimizes that code using baseline or optimizing compiler.
Cython
Cython是一种编程语言,它是python和C的超集。它是用C和python编写的。它的设计目的是通过python语法和可选的c语法提供类似c的性能。Cython是一种编译语言,因为它生成C代码并由C编译器编译。我们可以在Cython中编写与默认python或CPython中类似的代码,区别是:
Cython允许我们编写可选的额外C代码,
在Cython中,我们的python代码在内部被翻译成C代码,以便C编译器可以编译它。虽然Cython的执行速度快得多,但与原始C语言的执行速度相比还是差得多。这是因为Cython必须调用CPython解释器和CPython标准库来理解编写的CPython代码
吉通/吉通
Java实现的python编程语言。它是用Java和python编写的。在这里,我们的python代码首先被编译为Java字节码,然后由JVM或Java虚拟机将字节码解释为特定于平台的操作。这类似于Java代码的执行方式:Java代码首先被编译为中间字节码,然后由JVM将该字节码解释为特定于平台的操作
PyPy
RPython implementation of python programming language. It is written in a restricted subset of python called Restricted Python (RPython). PyPy runs faster than CPython because to interpret bytecode, PyPy has a Just-in-time Compiler (Interpreter + Compiler) while CPython has an Interpreter. So JIT Compiler in PyPy can execute Python bytecode line-by-line like an Interpreter to reduce start up time, but if encounters with a hot section with repeatedly executed line of code then optimizes that code using Baseline or Optimizing Compiler.
JIT Compiler in a Nutshell: Compiler in Python translates our high level source code into bytecode and to execute bytecode, some implementations have normal Interpreter, some have Just-in-time Compiler. To execute a loop which runs say, million times i.e. a very hot code, initially Interpreter will run it for some time and Monitor of JIT Compiler will watch that code. Then when it gets repeated some times i.e. the code becomes warm* then JIT compiler will send that code to Baseline Compiler which will make some assumptions on variable types etc. based on data gathered by Monitor while watching the code. From next iterations if assumptions turn out to be valid, then no need to retranslate bytecode into machine code, i.e. steps can be skipped for faster execution. If the code repeats a lot of times i.e. code becomes very hot then JIT compiler will send that code to Optimizing Compiler which will make more assumptions and will skip more steps for very fast execution.
JIT Compiler Drawbacks: initial slower execution when the code gets analysed, and if assumptions turn out to be false then optimized compiled code gets thrown out i.e. Deoptimization or Bailing out happens which can make code execution slower, although JIT compilers has limit for optimization/deoptimization cycle. After certain number of deoptimization happens, JIT Compiler just does not try to optimize anymore. Whereas normal Interpreter, in each iteration, repeatedly translates bytecode into machine code thereby taking more time to complete a loop which runs say, million times
IronPython
c#实现的python,针对。net框架
Ruby Python
支持Ruby平台
蟒蛇Python
python和R编程语言用于科学计算,如数据科学、机器学习、人工智能、深度学习、处理大量数据等。大量的库,如scikit-learn, tensorflow, pytorch, numba, pandas, jupyter, numpy, matplotlib等都可以使用这个包
Stackless
Python for并发
为了测试每个实现的速度,我们编写了一个程序,使用N值50,000调用integrate_f 500次,并记录几次运行的执行时间。下表显示了基准测试结果:
Implementation |
Execution Time (seconds) |
Speed Up |
CPython |
9.25 |
|
CPython + Cython |
0.21 |
44x |
PyPy |
0.57 |
16x |