4 ms·
SIMD is true, but the original guess is correct, and that effect is bigger! using_map is faster because it's not allocating: it's re-using the input array. Tha
by ridiculous_fish 2y ago
SIMD is true, but the original guess is correct, and that effect is bigger!
using_map is faster because it's not allocating: it's re-using the input array. That is, it is operating on the input `v` value in place, equivalent to this:
pub fn using_map(mut v: Vec<i32>) -> Vec<i32> {
v.iter_mut().for_each(|c| *c += 1);
v
}
This is a particularly fancy optimization that Rust can perform.
- skitter 2y agoThis can even turn the following code into a no-op: vec_of_u32.into_iter().map(f32::from_bits).collect()
- arllk 2y agoEven when passing the array as a borrow instead of a clone[1], map still auto-vectorizes and performs the new allocation in one go, avoiding the bounds check and possible calls to grow_one [1] https://godbolt.org/z/K9z6PvdYh https://godbolt.org/z/K9z6PvdYh