Python performance: Hello pythran, mypyc, nuitka.

This commit is contained in:
Julien Palard 2023-06-22 12:37:49 +02:00
parent a95bc5fe97
commit 5decb1d3ab
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
8 changed files with 89 additions and 5 deletions

View File

@ -3,5 +3,5 @@ def collatz_length(n):
return 0
if n % 2 == 0:
return 1 + collatz_length(n // 2)
elif n % 2 == 1:
else:
return 1 + collatz_length(n * 3 + 1)

View File

@ -6,5 +6,5 @@ def collatz_length(n: cython.uint) -> cython.uint:
return 0
if n % 2 == 0:
return 1 + collatz_length(n // 2)
elif n % 2 == 1:
else:
return 1 + collatz_length(n * 3 + 1)

View File

@ -0,0 +1,7 @@
def collatz_length(n: int) -> int:
if n == 1:
return 0
if n % 2 == 0:
return 1 + collatz_length(n // 2)
else:
return 1 + collatz_length(n * 3 + 1)

View File

@ -0,0 +1,7 @@
def collatz_length(n):
if n == 1:
return 0
if n % 2 == 0:
return 1 + collatz_length(n // 2)
else:
return 1 + collatz_length(n * 3 + 1)

View File

@ -7,5 +7,5 @@ def collatz_length(n):
return 0
if n % 2 == 0:
return 1 + collatz_length(n // 2)
elif n % 2 == 1:
else:
return 1 + collatz_length(n * 3 + 1)

View File

@ -0,0 +1,8 @@
#pythran export collatz_length(int)
def collatz_length(n):
if n == 1:
return 0
if n % 2 == 0:
return 1 + collatz_length(n // 2)
else:
return 1 + collatz_length(n * 3 + 1)

View File

@ -444,9 +444,9 @@ $ python -m pyperf timeit \
```shell
$ cythonize --inplace collatz_length.py
#!cythonize --inplace include/collatz_length.py
```
#!cythonize --inplace include/collatz_length.py
```shell
$ python -m pyperf timeit \
@ -518,10 +518,69 @@ $ python -m pyperf timeit \
# mypyc
mypyc est un compilateur qui s'appuie sur les annotationes de type mypy :
```python
#!cat include/collatz_length_mypy.py
```
## mypyc demo
```shell
$ mypyc include/collatz_length_mypy.py
#!mypyc include/collatz_length_mypy.py
```
```shell
$ python -m pyperf timeit \
> -s 'from collatz_length_mypy import collatz_length' \
> 'collatz_length(837799)'
#!cache -- python -m pyperf timeit --fast -s 'from collatz_length_mypy import collatz_length' 'collatz_length(837799)'
```
# Pythran
pythran est un compilateur pour du code scientifique :
```python
#!cat include/collatz_length_pythran.py
```
## Pythran demo
```shell
$ pythran include/collatz_length_pythran.py
#!pythran include/collatz_length_pythran.py
```
```shell
$ python -m pyperf timeit \
> -s 'from collatz_length_pythran import collatz_length' \
> 'collatz_length(837799)'
#!cache -- python -m pyperf timeit --fast -s 'from collatz_length_pythran import collatz_length' 'collatz_length(837799)'
```
# Nuitka
# https://github.com/pfalcon/awesome-python-compilers
Aussi un compilateur, aussi utilisable pour distribuer une application.
```shell
$ python -m nuitka --module collatz_length_nuitka.py
#!python -m nuitka --module include/collatz_length_nuitka.py >/dev/null
```
```shell
$ python -m pyperf timeit \
> -s 'from collatz_length_nuitka import collatz_length' \
> 'collatz_length(837799)'
#!cache --force -- python -m pyperf timeit --fast -s 'from collatz_length_nuitka import collatz_length' 'collatz_length(837799)'
```
# Et d'autres
https://github.com/pfalcon/awesome-python-compilers
# Hand crafted C

View File

@ -3,3 +3,6 @@ cython
snakeviz
scalene
numba
mypy
pythran
nuitka