Search

Codon, a Python Compiler written with C++ and LLVM

High performance and no overhead! In a test I did, the performance gain was greater than 600%.


Codon, a Python Compiler written with C++ and LLVM


Codon is a high-performance Python compiler that compiles Python code to native machine code without any runtime overhead.

The compiler was written with C++ and uses LLVM as the final assembly code optimizer. Unlike Python, Codon supports native multithreading, which can lead to even greater speedups.


Installation

You can compile Codon on your own machine, however there are precompiled binaries for Linux and macOS.

In the case of Linux just run this command below:

You must have cURL installed.

/bin/bash -c "$(curl -fsSL https://exaloop.io/install.sh)"

At the end of the installation it will ask you to confirm the addition of the binary to your $PATH variable, press y for yes. If it doesn’t work, even opening it in a new terminal, as he advises, run the following commands:

echo 'export PATH=${HOME}/.codon/bin:${PATH}' >> ~/.bashrc
exec $SHELL

To test run:

codon --version

Usage

Suppose you have this code Python which is a Fibonacci function, you can interpret the code with Codon itself:

def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()
fib(1000)

To run without compiling:

codon run fib.py

However, the performance will not be very good. The correct thing is to compile this code to a binary and then run the binary, example:

codon build -release -exe fib.py
./fib

You can still compile with [LLVM] optimization(https://terminalroot.com/tags#llvm)

codon build -release -llvm fib.py
./fib

Analyzing the performance

When we used a for loop with 1 million cycles in Python, as we did in this video, Codon was about 600% faster than native interpreter (version 3.10.9).

Test file/code can be obtained here.

Result using /usr/bin/python

time python main.py
1000000Ok
real   0m6,264s
user   0m3,530s
sys    0m2,415s

That is, actual execution: more than 6 seconds. 😞

Result using codon

codon build -release -exe main.py
time ./main
1000000Ok
real   0m0.795s
user   0m0.254s
sys    0m0,063s

Actual execution: in less than 1 second!!! 😲


Useful links


python cpp llvm


Share



Comments