Ndarray calculation is not fast, why?

I have two variables which are named x and y and their type is ArrayBase<ViewRepr<&f64>, Dim<[usize; 1]>>

calculation 1:
let z = (&x - &y).map(|v| v.abs()).sum();
calculation 2:
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 ndarray with 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.

1 Like

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.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.