Hi,

I am exploring Julia's map() and broadcast() functions. I did a simple 
implementation of MAPE (mean absolute percentage error) using broadcast() 
and map(). Interestingly, the difference in performance was huge.

A = rand(5_000_000)
F = rand(5_000_000)

_f(a,f) = (a - f) / a

function mape3(A, F)
# A - actual target values
# F - forecasts (model estimations)

  tmp = similar(A)
  broadcast!(_f, tmp, A, F)
  100 * sumabs(tmp) / length(A)

end

function mape4(A, F)
# A - actual target values
# F - forecasts (model estimations)

  tmp = similar(A)
  map!(_f, tmp, A, F)
  100 * sumabs(tmp) / length(A)

end

@time mape3(A,F) # after JIT warm-up
  0.038686 seconds (8 allocations: 38.147 MB, 2.25% gc time)
876.4813057521973

@time mape4(A,F) # after JIT warm-up
  0.457771 seconds (20.00 M allocations: 343.323 MB, 11.29% gc time)
876.4813057521973

I wonder why map() is so much slower ?

Thanks,
Jan

Reply via email to