How to speed up this function?

Can anyone spot optimizations?

using FLoops

function go(arr)

record = zeros(Float16,1,9)
record_add = zeros(Float16,1,9)

uniq = unique(arr[:,1])

for (c_86, c_90, c_89_1, c_53, c_52, c_89, c_88) in collect(Iterators.product(20:10:300, 2:8, 2:8, 2:15, 2:15, 2:8, 2:8))
    
    sum_ = 0
    count = 0
    @floop for i in uniq

        sub = @view arr[(arr[:,1] .== i), :]

        idx0 = findfirst((sub[:,7] .>= c_88) .& (sub[:,8] .>= c_89) .& (sub[:,4] .>= c_52) .& (sub[:,5] .>= c_53))

        if !isnothing(idx0)
            @fastmath count += 1

            t_0 = sub[idx0, 3]

            if size(sub)[1] > (idx0 + 3)
                sub_ = @view sub[(idx0 + 3):end,:]                                                                                

                idx1 = findfirst((sub_[:,8] .>= c_89_1) .| (sub_[:,9] .>= c_90) .| (sub_[:,6] .>= c_86))

                if !isnothing(idx1)

                    t_1 = sub_[idx1,3]
                    @fastmath sum_ += (t_1 - t_0) - (t_1 - t_0) * 0.3

                else
                    t_1 = sub[end,3]
                    @fastmath sum_ += (t_1 - t_0) - (t_1 - t_0) * 0.3
                end
            else
                t_1 = sub[end,3]
                @fastmath sum_ += (t_1 - t_0) - (t_1 - t_0) * 0.3
            end

        end

    end
    
    @fastmath earn /= 100
    record_add[1,:] = [c_88, c_89, c_52, c_53, c_89_1, c_90, c_86, count, sum_]
    record = vcat(record, record_add)

end

return record

end

Hm…what’s about

idx0 = @views findfirst((sub[:,7] .>= c_88) .& (sub[:,8] .>= c_89) .& (sub[:,4] .>= c_52) .& (sub[:,5] .>= c_53))
#same for idx1

?

Make sure not to use collect here, that will allocate an array of all the combinations, instead of combining them on the fly.

1 Like