python perf : rust

This commit is contained in:
Julien Palard 2023-10-09 16:00:49 +02:00
parent 1b5208e07c
commit 5e80699b87
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
3 changed files with 37 additions and 2 deletions

View File

@ -0,0 +1,16 @@
// rustimport:pyo3
use pyo3::prelude::*;
#[pyfunction]
fn collatz_length(n: u64) -> u64 {
if n == 1 {
return 1;
}
if n % 2 == 0 {
return 1 + collatz_length(n / 2);
}
else {
return 1 + collatz_length(n * 3 + 1);
}
}

View File

@ -575,6 +575,24 @@ $ cythonize -i examples/collatz_length_cython_to_c.pyx
```
## TODO
# Hand crafted rust
https://docs.python.org/3.12/howto/perf_profiling.html#perf-profiling
```rust
#!sed -n '/pyfunction/,$p' examples/collatz_length_rs.rs
```
## with rustimport
```bash
$ pip install rustimport
```
## with rustimport
```bash
#!cache pyperf timeit --setup 'from examples.collatz_length import collatz_length' 'collatz_length(837799)'
#!cache pyperf timeit --setup 'import rustimport.import_hook; from examples.collatz_length_rs import collatz_length' 'collatz_length(837799)'
```

View File

@ -7,3 +7,4 @@ mypy
pythran
nuitka
line_profiler
rustimport