Improving Performance of a Loop

You shouldn’t handle this by forcing your containers to have an abstract type (this should never be necessary), but by allowing your containers to adapt to the input types. For example, you could write

vals = zeros(typeof(θ),size(Mat3)[1:2])

or something like that. This is not the best way, however. Instead, don’t preallocate at all, you can just write

log_vals = -θ.*τ .+ γ
vals = exp.(log_vals)

(no need to first call exp and then log on the exped values.)

4 Likes