# How to speed up this function?

**URL:** <https://discourse.julialang.org/t/how-to-speed-up-this-function/66030>\
**Category:** Performance\
**Created:** [August 8, 2021, 4:47pm UTC](https://discourse.julialang.org/t/how-to-speed-up-this-function/66030 "2021-08-08T16:47:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![le\_go](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/le_go/32/27984_2.png) [@le\_go](https://discourse.julialang.org/u/le_go)\
**Post date:** [August 8, 2021, 4:47pm UTC](https://discourse.julialang.org/t/how-to-speed-up-this-function/66030/1 "2021-08-08T16:47:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [August 8, 2021, 5:05pm UTC](https://discourse.julialang.org/t/how-to-speed-up-this-function/66030/2 "2021-08-08T17:05:27Z")

</div>

Hm…what’s about

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

```

?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 8, 2021, 6:01pm UTC](https://discourse.julialang.org/t/how-to-speed-up-this-function/66030/3 "2021-08-08T18:01:30Z")

</div>

> [@le\_go](#):
>
> `collect(Iterators.product`

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