# \[Turing.jl\] Understanding running times in Turing.jl? Beyond @time

**URL:** https://discourse.julialang.org/t/turing-jl-understanding-running-times-in-turing-jl-beyond-time/118154
**Category:** Performance
**Tags:** question, turing, bayesian-inference, speed-optimization
**Created:** [August 13, 2024, 5:59pm UTC](https://discourse.julialang.org/t/turing-jl-understanding-running-times-in-turing-jl-beyond-time/118154 "2024-08-13T17:59:34Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![FJRubio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fjrubio/32/206789_2.png) [@FJRubio](https://discourse.julialang.org/u/FJRubio)
#### Post date: [August 13, 2024, 5:59pm UTC](https://discourse.julialang.org/t/turing-jl-understanding-running-times-in-turing-jl-beyond-time/118154/1 "2024-08-13T17:59:34Z")

</div>

This is a general question about understanding running times in `sample()` from `Turing.jl` when I know how long it takes to evaluate a custom log-likelihood function.

I have a custom log-likelihood function, `my_loglik()`, for a p-dimensional parameter (p=7 in this specific example). Evaluating `my_loglik(vec)`, for a specific parameter value `vec` takes 0.125 seconds. I have a Bayesian model with `Normal(0,1)` priors on all parameters and my custom log-likelihood `my_loglik()`, which I am implementing as follows:

```julia
@model function my_model(my_loglik)

    # Sample from priors
    par ~ filldist(Normal(0, 1), 7)
    
    # Add log-likelihood to the model
    Turing.@addlogprob!(my_loglik(par))
end

model = my_model(my_loglik)

```

Now, timing the sampling using `NUTS()` algorithm with specific initial point (an educated guess in the high probability region) for 100 iterations

```julia
# Run the MCMC sampler
Random.seed!(123)
N = 100

@time sample(model, NUTS(), N; init_params=init)

```

The code seems to be doing what it is supposed to be doing (by looking at the output), and it takes ~850 seconds (over several test runs), so, roughly 8.5 seconds per iteration. I find this to be quite slow in view of the time it takes to evaluate `my_loglik`. This would mean 50+ evaluations per iteration, leaving room for other operations.

Is there something I can do to make the sampling faster? Or anything wrong with the way I am defining `my_model`?

Apologies if my question is too vague.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [August 13, 2024, 6:29pm UTC](https://discourse.julialang.org/t/turing-jl-understanding-running-times-in-turing-jl-beyond-time/118154/2 "2024-08-13T18:29:16Z")

</div>

There are a few things you can explore. One is using ReverseDiff as your AD backend. Seven parameters may not be the point where it matters much, but if you wan to scale higher, you will need reverse mode AD: [Performance Tips – Turing.jl](https://turinglang.org/docs/tutorials/docs-13-using-turing-performance-tips/)

It may not matter much, but `p` should be passed as a parameter to your model function. I am assuming you are passing values near the mode via `init`, but if you are not, the initial values might be sub-optimal. Aside from that, we would need to see your log likelihood function, as that is likely the source of your bottleneck.
