Rust, Python and Fish

Fish Discs

Python

numba

Rust

Going parallel

let rsamples :Mutex<Vec<Quat>> = Mutex::new(vec!());
let rpoints : Mutex<Vec<Quat>> = Mutex::new(vec!());
let mut pool = Pool::new(partitions as u32);
pool.scoped(|scoped| {
for i in 0..partitions {
let bsamples = &rsamples;
let bpoints = &rpoints;
scoped.execute( move || {
let ns : usize = sample_size / partitions;
let mut tpoints : Vec<Quat> = vec!();
for p in points.iter() {
let aa = p.axis_angle();
// DO ALL THE WORK HERE
}
//println!("{}, {}", i, tpoints.len() );
let trval = sample(&tpoints, ns);
for q in trval.0.iter() {
let mut t = bsamples.lock().unwrap();
t.push(*q);
}
for q in trval.1.iter() {
let mut t = bpoints.lock().unwrap();
t.push(*q);
}
});
}
scoped.join_all();
});
(rsamples.into_inner().unwrap(), rpoints.into_inner().unwrap())

Talking to Python

use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
/// A Python module implemented in Rust.
#[pymodule]
fn string_sum(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}

How fast is fast?

python -m unittest test.poisson.Poisson.test_speed_rust
.
----------------------------------------------------------------------
Ran 1 test in 0.013s
OK
python -m unittest test.poisson.Poisson.test_speed_numba
.
----------------------------------------------------------------------
Ran 1 test in 1.782s
OK
Ran 1 test in 373.959s
...

So why bother?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store