I have two variables which are named
y and their type is
ArrayBase<ViewRepr<&f64>, Dim<[usize; 1]>>
let z = (&x - &y).map(|v| v.abs()).sum();
let mut z = 0.0; azip!((&a in &x, &b in &y) z += (a - b).abs());
I find 'calculation 2' is much faster than 'calculation 1', why?
I have tried to replace all
vec, it is faster than 'calculation 2'.
You need to profile the code for sure, but I guess it's because the former have 2 intermediate allocations and copies between them while the latter is zero-allocation.
Obligatory: are you compiling with
--release? Without optimizations things will be slow for irrelevant reasons.
Are you measuring it with bencher or criterion? A single
timer.elapsed() is very skewed due to caches, CPU frequency boosts, multitasking, and compiler potentially reordering or deleting code.
Have you compared the assembly the compiler generated for each?
Do you mean
z += instead of
d += ?
yes, you are right; thank you.