Codon 是一個能將符合規範的 Python 程式碼轉換成機械碼的編譯器(compiler)。工具本身是可擴充的,所以可以逐步延伸工具的容納範圍。比較一下 Numba

FeatureCodonNumba
Compilation ApproachTranspiles Python code into native machine code via a standalone compiler (ahead-of-time).JIT compilation at runtime, optimizing functions as needed.
Language FeaturesSupports a statically-typed subset of Python; limited to certain Python features.Supports a wide range of Python features, though with some limitations for advanced features.
PerformanceProvides high performance by compiling to native machine code.Offers significant speedups, especially for numerical computations, but is bounded by Python runtime limitations.
InteroperabilityRequires adaptation to static typing; limited support for Python libraries.Works within the Python ecosystem, supporting many libraries with minimal code changes.
Use CasesBest for scenarios requiring maximum performance and where code can be adapted to static typing.Ideal for incrementally optimizing existing Python codebases, especially in scientific computing.

速度上可以有感提升?不過要確定程式符合其子集語法的要求。

from time import time
 
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)
 
t0 = time()
ans = fib(40)
t1 = time()
print(f'Computed fib(40) = {ans} in {t1 - t0} seconds.')

實際執行速度

$ python3 fib.py
Computed fib(40) = 102334155 in 17.979357957839966 seconds.
$ codon run -release fib.py
Computed fib(40) = 102334155 in 0.275645 seconds.