We first write in a small Python module which defines a function using Transonic. We are going to run this module and to modify it during the Ipython session.
%% Cell type:code id: tags:
``` python
src="""
from transonic import boost
@boost
def func(n: int):
return 2 * n
if __name__ == "__main__":
print("result func(1):", func(1))
"""
withopen("tmp.py","w")asfile:
file.write(src)
```
%% Cell type:markdown id: tags:
Let's first clean all Transonic cache files...
%% Cell type:code id: tags:
``` python
paths=Path("__pythran__").glob("tmp*.*")
forpathinpaths:
path.unlink()
```
%% Cell type:markdown id: tags:
We first use Transonic in the default mode for which Pythran is not used at run time.
%% Cell type:code id: tags:
``` python
set_compile_at_import(False)
```
%% Cell type:code id: tags:
``` python
runtmp.py
```
%%%% Output: display_data
%%%% Output: stream
Module /home/pierre/Dev/transonic/doc/ipynb/executed/tmp.py has not been compiled for Transonic-Pythran
%%%% Output: stream
result func(1): 2
%% Cell type:code id: tags:
``` python
runtmp.py
```
%%%% Output: stream
result func(1): 2
%% Cell type:markdown id: tags:
Let's now switch on the Transonic mode with Pythran compilations at import time.
%% Cell type:code id: tags:
``` python
set_compile_at_import(True)
```
%% Cell type:markdown id: tags:
We then rerun the tmp.py module:
%% Cell type:code id: tags:
``` python
%runtmp.py
```
%%%% Output: stream
Running transonic on file /home/pierre/Dev/transonic/doc/ipynb/executed/tmp.py... WARNING 1 files created or updated needs to be pythranized
Running transonic on file /home/pierre/Dev/transonic/doc/ipynb/executed/tmp.py...
%%%% Output: stream
1 files created or updated needs to be pythranized
Ok so here, we see that Pythran compilations are quite long! In particular compared to Numba, PyPy or Julia JIT compilations! But after these few seconds, it should very efficient...