python perf : rust
This commit is contained in:
parent
1b5208e07c
commit
5e80699b87
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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)'
|
||||
```
|
||||
|
|
|
@ -7,3 +7,4 @@ mypy
|
|||
pythran
|
||||
nuitka
|
||||
line_profiler
|
||||
rustimport
|
||||
|
|
Loading…
Reference in New Issue