Help with optimising a simple function

This is the faster I got can you try

@fastmath function theorCovEff3(ts,k,l,ln,α)
    K(s,t) = ifelse(α ≈ 1.0, 2min(s,t) , (s^α + t^α - abs(s-t)^α))
    @inbounds @inline function incrCov(ts,i,j,k,l,K) 
        a, b, c, d = ts[i], ts[j], ts[k], ts[l]
        K(a,b) + K(a+c,b+d) - K(a,b+d) - K(a+c,b)
    end

    if k > l
        k, l = l, k
    end
    s1 = 0.0
    s2 = 0.0
    @simd for h in 2:ln-l
        s1 += incrCov(ts,1,h,k,l,K)^2*(ln-l-h+1)
    end
    @simd for h in 1:ln-k
        s2 += incrCov(ts,h,1,k,l,K)^2*ifelse(h <= l-k+1 , (ln-l) , (ln-k-h+1))
    end

    return 2/((ln-k)*(ln-l)) * ( s1 + s2)
end

on my computer : numba : 27 μs, julia : 24 μs, without fastmath I get same speed for both. Also, you’re doing a lot too much ops, maybe you know it but it is equivalent to

 @fastmath @inbounds function theorCovEff3(ts,k,l,ln,α::T) where T
    if k > l
        k, l = l, k
    end
    @inline K(α, a,b,c,d) =  α ≈ 1 ? 2min(a,b) + 2min(a+c,b+d) - 2min(a,b+d) - 2min(a+c,b) : abs(a-b-d)^α + abs(a+c-b)^α - abs(a-b)^α   - abs(a+c-b-d)^α 
    s1 = T(0)
    s2 = T(0)
    c = ts[k]
    d = ts[l]
    @inbounds @simd for h in 2:ln-l
        a = ts[1]
        b = ts[h]
        i1 =  K(α, a,b,c,d)
        s1 += i1*i1*(ln-l-h+1)
    end
    @inbounds @simd for h in 1:ln-k
        a = ts[h]
        b = ts[1]
        i2 =  K(α, a,b,c,d)
        s2 += i2*i2*ifelse(h <= l-k+1 , (ln-l) , (ln-k-h+1))
    end
    return T(2)/((ln-k)*(ln-l)) * ( s1 + s2)
end

which then leads to 8 μs on my end

8 Likes