I’ve been playing with writing things in different forms, for example the following:
function f_by_loop!(data,x) @inbounds for i in eachindex(data) data[i] = min(x,data[i]) end end
data .= min.(x,data) or
map!(y -> min(y,x), data, data).
where, for example,
These do the same thing and once I added
@inbounds to the loop, all three performed similarly.
The broadcast form is definitely the most concise and I’m guessing the least error prone.
My question is: Is broadcast also preferable to the manual loop because it does not unnecessarily specify an order of operation, making it easier for the compiler to optimize?