Hi there!
I recently started learning rust as I need an extreme speed up in a project that previously was written only in python.
The first (and simplest) function I translated simply calculates the norm of many 2d vectors. The arguments are a vector of x components and one of y components which are both of length n. The output is a vector of length n containing all the norms.
To my surprise even the first and simple implementation was 2.5 times faster than the fastest python solution:
pub fn calc_norm(xs: &Vec<f64>, ys: &Vec<f64>) -> Vec<f64>{
let mut norm: Vec<f64> = vec![0.0; xs.len()];
for i in 0..xs.len(){
norm[i] = ((xs[i]).powi(2) + (ys[i]).powi(2)).sqrt();
}
return norm
}
This however is where my questions start to arise.
-
Firstly, what is the way of dealing with a problem like this in rust? In python my usual approach was to always look for a numpy solution, as this was always faster. Does this apply to rust too, i.e. should I just look for a crate that does this? And if this is the case, how would one find the appropriate crate?
-
Secondly, how can I vectorize this function? I read through many blogs/questions/forum posts and tried their solutions but it didn't work out at all. It actually only took longer to run my code. I thought the assert statement in the code below was needed (to allow for auto-vectorization) so the compiler knows that we are never out bounds of the array (is this true?). Also I have read that iterators would be much faster, but it actually made my code slower - so what went wrong here?
pub fn calc_norm2(xs: &Vec<f64>, ys: &Vec<f64>) -> Vec<f64>{
assert_eq!(xs.len(), ys.len(), "Can't calculate the 2d norm if the number of x and y components doesn't match");
let mut norm: Vec<f64> = vec![0.0; xs.len()];
for (i, rs) in xs.iter().zip(ys.iter()).enumerate() {
norm[i] = ((rs.0).powi(2) + (rs.1).powi(2)).sqrt();
}
return norm
}
- My Final question towards this topic is, what are general principals to follow in rust to make my code as fast as possible?
Thank you in advance for your answer!