Cached Just-In-Time compilation
With Transonic, one can use the Ahead-Of-Time compiler Pythran in a Just-In-Time mode. It is really the easiest way to speedup a function with Pythran, just by adding a decorator! And it works also in notebooks!
import numpy as np
from transonic import jit
def func0(a, b):
return a + b
@jit
def func1(a: int, b: int):
print("b", b)
return np.exp(a) * b * func0(a, b)
if __name__ == "__main__":
from time import sleep
a = b = np.zeros([2, 3])
for i in range(20):
print(f"{i}, call with arrays")
func1(a, b)
print(f"{i}, call with numbers")
func1(1, 1.5)
sleep(1)
Note that it can be very convenient to use type hints and
@jit
in order to avoid multiple warmup periods:
from transonic import jit, Type
T = Type(int, float)
@jit()
def func(a: T, b: T):
return a * b
if __name__ == "__main__":
from time import sleep
a_i = b_i = 1
a_f = b_f = 1.0
for _ in range(10):
print(_, end=",", flush=True)
func(a_i, b_i)
sleep(1)
print()
for _ in range(10):
print(_, end=",", flush=True)
func(a_f, b_f)
sleep(1)
If the environment variable TRANSONIC_COMPILE_AT_IMPORT
is set,
transonic compiles at import time the functions with type hints.