Which part of these code I can change to improve the run speed and reduce the gc time?

For me on Julia 1.7 these closures are inferred fine and I don’t see gc activity. What version did you test?

It is a pretty unusual pattern that you’re using here, though. A closure wrapping an array in order to be called from a higher-order function, which just iterates the array.

In your case, this could all boil down to ifelse.(datas .> 0.5, 1, -1). Or if you want to use an anonymous function approach, maybe this

map(datas) do value
      value > 0.5 ? 1 : -1
end

Or even more complex if you really want to separate the value and function specification:

iterfunc(f, data) = [f(x) ? 1 : -1 for x in data]
iterfunc(>(0.5), rand(1000))
3 Likes