# How to speed up these functions?

**URL:** <https://discourse.julialang.org/t/how-to-speed-up-these-functions/65728>\
**Category:** Performance\
**Tags:** function\
**Created:** [August 3, 2021, 7:40am UTC](https://discourse.julialang.org/t/how-to-speed-up-these-functions/65728 "2021-08-03T07:40:58Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 5, 2021, 6:15am UTC](https://discourse.julialang.org/t/how-to-speed-up-these-functions/65728/19 "2021-08-05T06:15:26Z")

</div>

I’m getting some puzzling results. Consider this:

```julia
using LoopVectorization

t = [26, 94]
c = [26, 94]
x = collect([25 12]')

α = 2.85
β = [-13.3, -0.006]

function logℒ_fast(α, β, t, c, x)
    eα = abs(α)
    n, k = size(x)
  
    (n == length(t) == length(c) && length(β) == k + 1) || throw(DimensionMismatch())
    s = zero(typeof(α))
    @inbounds for i in 1:n
      xb = sum(@inbounds(x[i, j] * β[j+1]) for j in 1:k) + β[1]
      ti = t[i]
      s += (1 - (c[i] == ti)) * (log(eα) + (eα - 1) * log(ti) + xb) - ti^eα * exp(xb)
    end
    return s
end

function logℒ_fast_turbo(α, β, t, c, x)
    eα = abs(α)
    n, k = size(x)
  
    (n == length(t) == length(c) && length(β) == k + 1) || throw(DimensionMismatch())
    s = zero(typeof(α))
    @turbo for i in 1:n
      xb0 = 0.0
      for j in 1:k
        xb0 += x[i,j] * β[j+1]
      end
      xb = xb0 + β[1]
      ti = t[i]
      s += (1 - (c[i] == ti)) * (log(eα) + (eα - 1) * log(ti) + xb) - ti^eα * exp(xb)
    end
    return s
end

```

Those are the same functions you defined above, however:

```julia
julia> logℒ_fast_turbo(α, β, t, c, x)
NaN

julia> logℒ_fast(α, β, t, c, x)
-0.6702178908853803

```

I can’t figure out what’s going on here.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-speed-up-these-functions/65728)._
