Python perfs: better caching.

This commit is contained in:
Julien Palard 2023-06-23 10:54:12 +02:00
parent 5decb1d3ab
commit 9d237e06dc
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
1 changed files with 38 additions and 16 deletions

View File

@ -400,11 +400,10 @@ python -m snakeviz phi5.prof
```shell
$ python -m pip install scalene
$ scalene phi5.py 100000
##!if [ ! -f output/phi5.html ]; then scalene include/phi5.py 100000 --html --outfile output/phi5.html --cli >&2; fi
##!if [ ! -f output/phi5-scalene.png ]; then cutycapt --min-width=1024 --delay=100 --url=file://$(pwd)/output/phi5.html --out=output/phi5-scalene.png; fi
#!if [ ! -f output/phi5.html ]; then ( cd include; scalene phi5.py 100000 --html --outfile ../output/phi5.html --cli >&2 ); fi
#!if [ ! -f output/phi5-scalene.png ]; then cutycapt --min-width=1024 --delay=100 --url=file://$(pwd)/output/phi5.html --out=output/phi5-scalene.png; fi
```
## Scalene
![](phi5-scalene.png)
@ -434,7 +433,6 @@ Cython est un dialecte de Python transpilable en C.
## Cython démo
#!rm -f include/*.so # Ensure we're not hitting the cythonized one here...
```shell
$ python -m pyperf timeit \
> -s 'from collatz_length import collatz_length'
@ -443,11 +441,10 @@ $ python -m pyperf timeit \
```
```shell
$ cythonize --inplace collatz_length.py
#!cythonize --inplace include/collatz_length.py
$ cythonize --inplace collatz_length_cython.py
#!if ! [ -f include/collatz_length_cython.*.so ] ; then cythonize --inplace include/collatz_length_cython.py; fi
```
```shell
$ python -m pyperf timeit \
> -s 'from collatz_length import collatz_length'
@ -461,8 +458,8 @@ $ python -m pyperf timeit \
```shell
$ cython -a collatz_length.py
#!cython -a include/collatz_length.py
#!cutycapt --min-width=1024 --delay=500 --url=file://$(pwd)/include/collatz_length.html --out=output/collatz_length.png
#!if ! [ -f include/collatz_length.html ] ; then cython -a include/collatz_length.py; fi
#!if ! [ -f output/collatz_length.png ] ; then cutycapt --min-width=1024 --delay=500 --url=file://$(pwd)/include/collatz_length.html --out=output/collatz_length.png; fi
```
![](collatz_length.png)
@ -478,7 +475,7 @@ $ cython -a collatz_length.py
$ cythonize --inplace collatz_length_annotated.py
```
#!cythonize --inplace include/collatz_length_annotated.py
#!if ! [ -f include/collatz_length_annotated.*.so ] ; then cythonize --inplace include/collatz_length_annotated.py; fi
```shell
$ python -m pyperf timeit \
@ -492,8 +489,8 @@ $ python -m pyperf timeit \
```shell
$ cython -a include/collatz_length_annotated.py
#!cython -a include/collatz_length_annotated.py
#!cutycapt --min-width=1024 --delay=500 --url=file://$(pwd)/include/collatz_length_annotated.html --out=output/collatz_length_annotated.png
#!if ! [ -f include/collatz_length_annotated.html ] ; then cython -a include/collatz_length_annotated.py; fi
#!if ! [ -f output/collatz_length_annotated.png ] ; then cutycapt --min-width=1024 --delay=500 --url=file://$(pwd)/include/collatz_length_annotated.html --out=output/collatz_length_annotated.png; fi
```
![](collatz_length_annotated.png)
@ -528,7 +525,7 @@ mypyc est un compilateur qui s'appuie sur les annotationes de type mypy :
```shell
$ mypyc include/collatz_length_mypy.py
#!mypyc include/collatz_length_mypy.py
#!if ! [ -f collatz_length_mypy.*.so ] ; then mypyc include/collatz_length_mypy.py; fi
```
```shell
@ -551,7 +548,7 @@ pythran est un compilateur pour du code scientifique :
```shell
$ pythran include/collatz_length_pythran.py
#!pythran include/collatz_length_pythran.py
#!if ! [ -f collatz_length_pythran.*.so ]; then pythran include/collatz_length_pythran.py; fi
```
```shell
@ -568,14 +565,14 @@ 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
#!if ! [ -f collatz_length_nuitka.*.so ]; then python -m nuitka --module include/collatz_length_nuitka.py >/dev/null; fi
```
```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)'
#!cache -- python -m pyperf timeit --fast -s 'from collatz_length_nuitka import collatz_length' 'collatz_length(837799)'
```
@ -584,3 +581,28 @@ $ python -m pyperf timeit \
https://github.com/pfalcon/awesome-python-compilers
# Hand crafted C
```c
#!sed -n '/int collatz_length/,/^$/p' include/my_collatz_length.c
```
Mais comment l'utiliser ?
## Hand crafted C
### Avec Cython
```cpython
#!cat include/collatz_length_cython_to_c.pyx
```
```shell
$ cythonize -i include/collatz_length_cython_to_c.pyx
#!if ! [ -f include/collatz_length_cython_to_c.*.so ] ; then cythonize -i include/collatz_length_cython_to_c.pyx; fi
```
```shell
$ python -m pyperf timeit \
> -s 'from collatz_length_cython_to_c import collatz_length' \
> 'collatz_length(837799)'
#!cache -- python -m pyperf timeit --fast -s 'from include.collatz_length_cython_to_c import collatz_length' 'collatz_length(837799)'
```