Just playing a bit here, and we can get the close to the fastest version using:
julia> function theorCovEff3(ts,k,l,ln,α)
K(s,t) = ifelse((α ≈ 1.0), 2min(s,t),(s^α + t^α - abs(s-t)^α))
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
k, l = ifelse(k > l, (l,k), (k,l))
N1 = h -> ln-l-h+1
N2 = h -> (h <= l-k+1) ? ( ln-l ) : ( ln-k-h+1 )
return 2/((ln-k)*(ln-l)) * (
sum( N1(h)*incrCov(ts,1,h,k,l,K)^2 for h in 2:ln-l; init=0 )
+ sum( N2(h)*incrCov(ts,h,1,k,l,K)^2 for h in 1:ln-k )
)
end
theorCovEff3 (generic function with 1 method)
julia> @b theorCovEff3(1:100, 2, 10, 100, 0.7)
21.193 μs
which is the original version just changing the k,l
assignment to remove the type-instability and changing the K(s,t)
definition to use ifelse
.
(I like the fact that @inbound
, @simd
, @fastmath
, etc, decorations are most of the times unnecessary)