diff --git a/python-perfs/include/collatz_length.py b/python-perfs/include/collatz_length.py index a265716..314123d 100644 --- a/python-perfs/include/collatz_length.py +++ b/python-perfs/include/collatz_length.py @@ -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) diff --git a/python-perfs/include/collatz_length_annotated.py b/python-perfs/include/collatz_length_annotated.py index 89b1ec1..123d8d9 100644 --- a/python-perfs/include/collatz_length_annotated.py +++ b/python-perfs/include/collatz_length_annotated.py @@ -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) diff --git a/python-perfs/include/collatz_length_mypy.py b/python-perfs/include/collatz_length_mypy.py new file mode 100644 index 0000000..c7c93ef --- /dev/null +++ b/python-perfs/include/collatz_length_mypy.py @@ -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) diff --git a/python-perfs/include/collatz_length_nuitka.py b/python-perfs/include/collatz_length_nuitka.py new file mode 100644 index 0000000..314123d --- /dev/null +++ b/python-perfs/include/collatz_length_nuitka.py @@ -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) diff --git a/python-perfs/include/collatz_length_numba.py b/python-perfs/include/collatz_length_numba.py index 5d47c60..e2c74a2 100644 --- a/python-perfs/include/collatz_length_numba.py +++ b/python-perfs/include/collatz_length_numba.py @@ -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) diff --git a/python-perfs/include/collatz_length_pythran.py b/python-perfs/include/collatz_length_pythran.py new file mode 100644 index 0000000..4654c21 --- /dev/null +++ b/python-perfs/include/collatz_length_pythran.py @@ -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) diff --git a/python-perfs/perfs.md b/python-perfs/perfs.md index c4f8df8..41dd3b7 100644 --- a/python-perfs/perfs.md +++ b/python-perfs/perfs.md @@ -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 diff --git a/python-perfs/requirements.txt b/python-perfs/requirements.txt index e81edb7..ebe0c3c 100644 --- a/python-perfs/requirements.txt +++ b/python-perfs/requirements.txt @@ -3,3 +3,6 @@ cython snakeviz scalene numba +mypy +pythran +nuitka