Recently my colleagues asked me to translate my code for estimation to Python. Making it run with any reasonable speed was truly a pain and advertisement for Julia, but at the end it became few times faster with numba. It is strange, because the algorithm is very elementary, you’d expect similar resulting LLVM. The main culprit seems to be here:
function theorCovEff3(ts,k,l,ln,α)
K(s,t) = (α ≈ 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
if k > l
k, l = l, k
end
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
Example use theorCovEff3(1:100, 10, 2, 100, 0.7)
. I’ve tried @inbounds
and @fastmath
, it does not help much. I don’t understand why, but replacing sum
with loops makes it twice faster, but still slower than numba. Any tips?