# DifferentialEquations.jl How to speed up the solver

**URL:** <https://discourse.julialang.org/t/differentialequations-jl-how-to-speed-up-the-solver/83549>\
**Category:** Performance\
**Created:** [June 30, 2022, 4:38am UTC](https://discourse.julialang.org/t/differentialequations-jl-how-to-speed-up-the-solver/83549 "2022-06-30T04:38:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nalaka](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@Nalaka](https://discourse.julialang.org/u/Nalaka)\
**Post date:** [June 30, 2022, 4:38am UTC](https://discourse.julialang.org/t/differentialequations-jl-how-to-speed-up-the-solver/83549/1 "2022-06-30T04:38:42Z")

</div>

I am using Julia to solve set of differential equations of a omni-wheeled mobile robot. It took 1264.41 sec (21.07 min) to solve the system (But in mathematica, it took less than 4s). In the time, it showed 99% time is for the compilation. I would like to know is there a way that I can save the compiled file and reuse it/ any methods to speed up the solver. I am willing to use this model in a Reinforcement learning algorithm. Also, only the first four variables gave me the results matches with Mathematica or Python. Rest of the variables in Julia show as zero.  
I am using an i7 11th generation processor with a 16GB RAM.

The code is lengthy to post here. I am sharing google dive link.

> **[robo.jl](https://drive.google.com/file/d/1yGFAbRDX6-LW9-ReghRj7VlxNHxdaQd5/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [June 30, 2022, 6:29am UTC](https://discourse.julialang.org/t/differentialequations-jl-how-to-speed-up-the-solver/83549/2 "2022-06-30T06:29:43Z")

</div>

I think it is a huge beast and almost nobody can deal with this kind of code. You need to find a structure to it. For example, find an intermediate vector with as a function of parameters and ODE states and etc. Then define the derivatives as a matrix vector product. It can also be cascaded layers of these matrix vector multiplications. However, If you write explicitly as it is now almost no one can reason about it.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 30, 2022, 8:22am UTC](https://discourse.julialang.org/t/differentialequations-jl-how-to-speed-up-the-solver/83549/3 "2022-06-30T08:22:58Z")

</div>

> [@tomaklutfu](#):
>
> I think it is a huge beast

A huge beast is almost an understatement 😅, the dynamics definition is over 45k lines of code… I assume that comes from some symbolic code generation? Perhaps you could perform this code generation differently, e.g., using common sub-expression elimination?

---

<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:** [June 30, 2022, 9:32am UTC](https://discourse.julialang.org/t/differentialequations-jl-how-to-speed-up-the-solver/83549/4 "2022-06-30T09:32:26Z")

</div>

```julia
sol =@time solve(prob,BS3(),saveat=10/200000,abstol=1e-8,reltol=1e-8)

```

What’s the reason for using `BS3`? That’s a terrible choice for low tolerances, the docs highly highly recommend against doing this. And with such a small `saveat`, it’s better to just use the continuous interpolation.

```julia
sol =@time solve(prob,Vern9(),abstol=1e-8,reltol=1e-8)

```

is going to be a lot faster. But yes, the main issue here is that 45k lines of code will take awhile to compile. I’d recommend using a lower optimization level if you’re going to do this, or just don’t generate such a large code and keep loops rolled 😅
