@jialin.huang
FRONT-ENDBACK-ENDNETWORK, HTTPOS, COMPUTERCLOUD, AWS, Docker
To live is to risk it all Otherwise you are just an inert chunk of randomly assembled molecules drifting wherever the Universe blows you

© 2024 jialin00.com

Original content since 2022

back
RSS

Python Execution — From Interpreters to Compilers and Packaging tools

Python Compilers

From a performance perspective: Static/AOT compilation > JIT compilation > JIT implementation > Interpreter

  • Static/AOT: Longer compile and build time, but better execution performance. Trades off some of Python's dynamic language advantages.
  • JIT compilation: A balance. Pre-compiles, so initial startup is slower and memory usage is higher.
  • Interpreter: Translates on-the-fly, fastest startup, easiest to debug and modify. Not suitable for CPU-intensive operations.

ToolsTypeFeatureSpecial
Static/AOT CompilerSlow first, high performance later
CythonStatic/AOT Compilerto C/C++Mainly produces modules, static type declaration
Shed SkinStatic/AOT Compilerto C++Mainly produces modules
NuitkaStatic/AOT Compilerto Cexecutables
PythranStatic/AOT CompilerOptimizes scientific computing codeEspecially suitable for NumPy programs
JIT compiler
NumbaJIT CompilerCompiles numerical computation codecan do partial AOT
JIT Implementation
PyPyJIT ImplementationDynamically compiles Python codeFully compatible with CPython, usually faster
PystonJIT ImplementationOptimizes Python executionCPython compatible
Interpreterstart fast, performance tradeoff
CPythonInterpreterRuns Python on PVM

note1
Standard Python implementation.
Most widely used.
Stackless Python
(Based on CPython)
InterpreterSupports microthreadsSpecial handling for high concurrency.
JythonInterpreterRuns Python on JVMcan use Java library
IronPythonInterpreterRuns Python on .NETcan use .NET library

note1

Note 1: In the Python world, VM and interpreter are often used interchangeably, but more precisely, "VM is considered part of the interpreter."

Can these tools be used in the same workflow? For example, using an interpreter but with a compiler underneath?

  • CPython (interpreter) can be used with Cython (compiler) to compile critical parts for improved performance.
  • Numba can be used in a CPython environment to provide JIT compilation for specific functions.

Ambiguity in CPython's definition

CPython is the default installed Python.

Wikipedia states

CPython can be defined as both an interpreter and a compiler as it compiles Python code into bytecode before interpreting it.

https://en.wikipedia.org/wiki/CPython

A Stack Overflow answer

CPython is the original Python implementation. It is the implementation you download from Python.org. People call it CPython to distinguish it from other, later, Python implementations, and to distinguish the implementation of the language engine from the Python programming language itself.

https://stackoverflow.com/questions/17130975/python-vs-cpython

While CPython is far from an AOT compiler, it could potentially be considered a JIT compiler?

There's still a significant difference between interpreters and JIT Compilers. The most obvious difference is that an interpreter uses a two-step process, translating to bytecode and then to machine code, rather than translating directly to final machine code.

AspectCPython as interpreterJIT compiler
TimingCompilation occurs before program execution, but only to bytecodeJIT compilation usually occurs during program runtime
TargetBytecode, run on VM or interpreterDirectly produces machine-readable code
OptimizationTranslates as encountered, no optimizationYes
DynamismCan execute code in string form at runtime using exec() or eval()More static analysis optimization, but more complex and memory-intensive

PyPy & Pyston's claim of CPython compatibility

This mainly means these implementations can run Python code written for CPython and produce the same results.

PyPy vs. Pyston compatibility differences:

  • PyPy offers high CPython compatibility and also supports both Python 2 and Python 3.
  • Pyston emphasizes its CPython compatibility, because it’s a fork from CPython.

JIT implementations like PyPy can sometimes approach or exceed certain JIT compilation scenarios.

  • Web servers

    In some benchmark tests with network application frameworks (like Tornado), PyPy's performance can sometimes approach Go language implementations of similar servers.

  • Dynamic type heterogeneous data
    • Suitable for NoSQL databases like MongoDB, Cassandra
    • data in different formats
  • Memory-intensive operations
    • In-memory databases
    • Large-scale graph processing
    • Image processing

Are CPU-intensive tasks more suitable for static compilation?

Yes, static compilation/AOT is generally more suitable.

For memory-intensive tasks like image processing, the choice between JIT and AOT depends on requirements. AOT is better for static, batch processing, while JIT is more suitable for user parameter adjustments or game-state calculations.

Specialized packages like TensorFlow and PyTorch often include both compilation modes.

SituationCPU intensiveIO IntensiveMemory Intensive
ToolStatic/AOT CompilationInterpreterDepends, but definitely NOT Interpreter

Packaging tools

Python packaging tools fall into two main categories:

  1. Standalone executable files: PyInstaller, cx_Freeze, py2exe, and py2app
  1. Modules and libraries: For reuse in other projects, requiring interpreters.
    1. setuptools: Suitable for small projects, creating modules for other directories to import.
    1. Nuitka: Packages into bin or exe files for direct execution.

      As seen in the compiler table above, Nuitka is both a compiler and a packaging tool. It's quite suitable when you want to improve program performance and easily distribute to end users.

    1. Poetry: Similar to native Python but runs in an isolated environment, like Docker.

pyproject.toml is a standard configuration file format used by many tools, but it's not absolute.

If simplicity is desired, using requirements.txt is fine.

toolsgoalneed interpreter
setuptools (sdist)Source distribution package
setuptools (bdist_wheel)Wheel file .whl
PyInstallerexecutable
cx_Freezeexecutable
py2exe.exe
py2app.app
NuitkaCompiles Python to native code
PoetryDependency management and packaging
FlitSimplifies packaging process

Anaconda conda cuda

Anaconda: A comprehensive Python tool with GUI

conda: Package and environment management tool (can be used independently)

cuda: A package for GPU operations

Machine learning tools like TensorFlow and PyTorch rely also on cuda libraries

Numba — JIT Compiler

Numba compiles Python → CUDA → GPU for accelerated computations

References

https://github.com/pypy/pypy?tab=readme-ov-file

https://en.wikipedia.org/wiki/CPython

https://stackoverflow.com/questions/17130975/python-vs-cpython

https://www.quora.com/How-fast-is-the-new-version-of-PyPy-interpreter-for-Python

http://zhaoxuhui.top/blog/2019/01/17/PythonNumba.html

https://zhuanlan.zhihu.com/p/60994299

https://orcahmlee.github.io/python/cython-basic/

https://discuss.python.org/t/aot-instead-of-jit/51849

https://www.codeconquest.com/blog/pyston-vs-pypy-similarities-and-differences/

https://lincolnloop.com/insights/speed-comparison-cpython-pypy-pyston/

https://bayareanotes.com/cuda-tutorial/

EOF