On Thu, Sep 8, 2016 at 12:03 PM, Dupont <[email protected]> wrote:

> Dear users,
>
> I would like to speed up the following code.
>
> function essai(n,s1,s2)
>   a = zeros(Int64,n)
>   ss1::Float64 = 0.
>   ss2::Float64 = 0.
>   @inbounds begin
>     for k=1:n
>       for i=1:length(s1)
>         ss1 = s1[i]
>         for j=1:length(s2)
>           ss2 = s2[j]
>           if ss1>ss2
>             a[k] += 1
>           end
>         end
>       end
>     end
>   end
> end
>
> a = rand(1,5000)
> b = rand(1,5000)
> essai(1,a,b)
> @time essai(200,a,b)
>
> On my computer, if I remove the *if*, it runs in 2ms and 13s otherwise.
> Could it be there is a way to speed it up without change the "algorithm"?
>

A few other note, you are not returning a so the functions is functionally
a no-op

Also what do you mean by `remove the if`, I don't think you can get such a
speed up if you just remove the branch and making `a[k] += 1` unconditional
(it will speed it up since it can do a few optimizations much more easily)
If you remove the if and the assignment then it's trivial for llvm to see
that the loop is no-op so it's not running any code at all.


>
> Thank you for your help
>
>
>

Reply via email to