I have an integer array I need to zero (0) each element. (Done in inner loop of algorithm, needs to be fast as possible).
Currently I do this:
ary.iter_mut().for_each(|m| *m = 0)
I'm translating the program to Crystal. Crystal|Ruby have the fill method, so I can do:
ary.fill(0)
Crystal just optimized it to fill integer arrays with zeros (per my request).
class Array(T)
def fill(value : T)
{% if Int::Primitive.union_types.includes?(T) %}
if value == 0
to_unsafe.clear(size)
else
fill { value }
end
{% else %}
fill { value }
{% end %}
end
end
Is there some comparable way to do this in Rust for integer arrays?
@jzakiya In case it isn't clear: the compiler emitting a memset is generally what you want, since that'll be the most optimized way to set memory on your platform.
So does this mean my original code snippet is the fastest way to do this?
If not can you provide an actual code snippet that is faster (I don't really understand the memset stuff).
memset is generally fastest, your original code already compiled into memset, so it would be the fastest code without further context.
But why we need context here? Optimization and performance are subtle topic and humans are generally bad at measure it from code. So you must actually benchmark your real code before any hand-optimization, otherwise your "optimization" may slow down your code.
So it seems the original code snippet may be the fastest.
(FYI: I always test my full code base when I make changes to it, not just code snippets.)
It would still be a nice feature (for users/programmers) to have an optimized fill method in the stdlib, to standardize these operations, and for synactic sugar to make source code shorter/clearer.