# DifferentialEquations.jl - Solving ODE with "alg\_hints=\[:stiff\]" is 20x slower, any alternatives?

**URL:** <https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750>\
**Category:** General Usage\
**Tags:** question, ode, algorithm, differentialequation\
**Created:** [February 5, 2024, 4:18pm UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750 "2024-02-05T16:18:48Z")\
**Posts on this page:** 9\
**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:** [February 5, 2024, 4:18pm UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/1 "2024-02-05T16:18:48Z")

</div>

I am solving several thousand systems of ODEs, with an explicit Jacobian, using `DifferentialEquations.jl`. These systems are similar to the competitive Lotka-Volterra model. In some cases (depending on the parameter values), Tsit5() method works well, but in some others the command solve() throws many “stiffness” and “Instability” warning messages.

> sol = solve(odeprob, Tsit5())

Increasing `maxiters` and tolerance does not help.

I found that using “alg\_hints=[:stiff]” produces a stable implementation, but it becomes 20 times slower.

> sol = solve(odeprob; alg\_hints=[:stiff])

Is there a faster alternative to using “alg\_hints=[:stiff]” while still considering stiffness?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [February 5, 2024, 4:32pm UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/2 "2024-02-05T16:32:48Z")

</div>

You can find recommended solvers here in the documentation

> **[ODE Solvers · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/solvers/ode_solve/#Stiff-Problems)**
>
> Documentation for DifferentialEquations.jl.

It is common that `alg_hints` is slower, as it might try different approaches at first. Which is especially relevant if the ODE solution itself is fast.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 5, 2024, 4:37pm UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/3 "2024-02-05T16:37:34Z")

</div>

what happens if you just solve without passing an algorithm or stiffness hint? you probably want to be using one of the methods that automatically switches between stiff and non stiff solvers.

---

<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:** [February 5, 2024, 4:42pm UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/4 "2024-02-05T16:42:23Z")

</div>

It continues throwing instability warning messages (and sometimes it enters into some sort of infinite loop, as it does not return a solution after a while).

> Warning: Instability detected. Aborting

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 6, 2024, 12:56am UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/5 "2024-02-06T00:56:52Z")

</div>

> [@FJRubio](#):
>
> I found that using “alg\_hints=[:stiff]” produces a stable implementation, but it becomes 20 times slower.

A stiff ODE solver on non-stiff ODEs is slower. That’s why non-stiff ODE solvers exist. You need to use then when you can. Is the auto-switch method not working well here?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 6, 2024, 8:06am UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/6 "2024-02-06T08:06:36Z")

</div>

If there are several thousands of unknowns, stiff methods will probably need to invert the jacobian (or some matrix related to it), and for this size, it can be slow. You can use GMRES for the linear solver and see if it helps

---

<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:** [February 6, 2024, 9:13am UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/7 "2024-02-06T09:13:04Z")

</div>

Thanks, Chris. I agree. In my case, the non-stiff solver just enters in some sort of infinite loop (once it starts throwing warning messages, it never stops). The non-stiff solver works, although it is 10x slower (naturally). The auto-switch method does not work either, it behaves the same as a non-stiff solver (throwing infinite warning messages).

Intriguingly, LSODA.jl has the same problem (infinite warning messages), while LSODA works well in R (yet, 50-100 times slower).

I am still investigating what is going on. Stiffness is a potential problem, or some extreme parameter values that lead to overflows.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [February 6, 2024, 7:45pm UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/8 "2024-02-06T19:45:40Z")

</div>

Speaking from my own experience, the most likely reason could be a small implementation mistake. Especially since it’s running in R.

If you could share the code or a MWE, people here can probably help you finding the discrepancy faster.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 6, 2024, 9:59pm UTC](https://discourse.julialang.org/t/differentialequations-jl-solving-ode-with-alg-hints-stiff-is-20x-slower-any-alternatives/109750/9 "2024-02-06T21:59:13Z")

</div>

> [@FJRubio](#):
>
> Intriguingly, [LSODA.jl](https://juliahub.com/ui/Packages/LSODA) has the same problem (infinite warning messages), while LSODA works well in R (yet, 50-100 times slower).

It’s the exact same solver. If the steps aren’t exactly the same, then there’s an implementation error. Did you try deSolveDiffEq.jl to double check?
