# Help with optimising a simple function

**URL:** https://discourse.julialang.org/t/help-with-optimising-a-simple-function/131459
**Category:** Performance
**Created:** [August 7, 2025, 5:06pm UTC](https://discourse.julialang.org/t/help-with-optimising-a-simple-function/131459 "2025-08-07T17:06:57Z")
**Posts on this page:** 1
**Showing post:** 16

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 8, 2025, 12:59pm UTC](https://discourse.julialang.org/t/help-with-optimising-a-simple-function/131459/16 "2025-08-08T12:59:25Z")

</div>

Just playing a bit here, and we can get the close to the fastest version using:

```julia-auto
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)

---

_[View the full topic](https://discourse.julialang.org/t/help-with-optimising-a-simple-function/131459)._
