๐งช Getting started with Codon¶
Install Codon¶
Run the following shell command in your terminal to install Codon:
/bin/bash -c "$(curl -fsSL https://exaloop.io/install.sh)"
Run your first program¶
Save the following code to file fib.py
:
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.')
Run the program in Codon:
codon run -release fib.py
Compile to an executable:
codon build -release -o fib fib.py
Run the executable via ./fib
.
Use Codon's "just-in-time" (JIT) compiler¶
Install Codon's JIT with pip
:
pip install codon-jit
Save the following code to file primes.py
:
import codon
from time import time
def is_prime_python(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
@codon.jit
def is_prime_codon(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
t0 = time()
ans = sum(1 for i in range(100000, 200000) if is_prime_python(i))
t1 = time()
print(f'[python] {ans} | took {t1 - t0} seconds')
t0 = time()
ans = sum(1 for i in range(100000, 200000) if is_prime_codon(i))
t1 = time()
print(f'[codon] {ans} | took {t1 - t0} seconds')
Run with Python:
python3 primes.py