# DifferentialEquations.solve() speed

**URL:** https://discourse.julialang.org/t/differentialequations-solve-speed/24329
**Category:** New to Julia
**Tags:** diffeq
**Created:** [May 17, 2019, 3:44pm UTC](https://discourse.julialang.org/t/differentialequations-solve-speed/24329 "2019-05-17T15:44:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dg.aragones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dg.aragones/32/11275_2.png) [@dg.aragones](https://discourse.julialang.org/u/dg.aragones)
#### Post date: [May 17, 2019, 3:44pm UTC](https://discourse.julialang.org/t/differentialequations-solve-speed/24329/1 "2019-05-17T15:44:54Z")

</div>

I’m using `DifferentialEquations.solve()` to solve an ODE. It involves a lot of numerical parameters and functions. I get the solution correctly using the `DifferentialEquations.VCABM3()` method (Adams-Bashford-Moulton). However, it takes a long time to solve the ODE at the first attempt, being fast after that if I repeat the computation.

Here are the times that I got:

> 10.455641 seconds (10.55 M allocations: 547.724 MiB, 6.30% gc time)  
> 0.006234 seconds (58.86 k allocations: 1.002 MiB)  
> 0.007289 seconds (58.86 k allocations: 1.002 MiB)  
> 0.006561 seconds (58.86 k allocations: 1.002 MiB)  
> 0.006577 seconds (58.86 k allocations: 1.002 MiB)  
> 0.005462 seconds (58.86 k allocations: 1.002 MiB)

The first time corresponds to the first attempt, and the following are shown after that.

Why might this be happening? Is there any way to get faster times from the first attempt?

Thanks in advance for any help.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [May 17, 2019, 3:51pm UTC](https://discourse.julialang.org/t/differentialequations-solve-speed/24329/2 "2019-05-17T15:51:18Z")

</div>

Compilation?

---

<div class="post-metadata">

### Author: ![dg.aragones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dg.aragones/32/11275_2.png) [@dg.aragones](https://discourse.julialang.org/u/dg.aragones)
#### Post date: [May 17, 2019, 4:02pm UTC](https://discourse.julialang.org/t/differentialequations-solve-speed/24329/3 "2019-05-17T16:02:58Z")

</div>

The times that I’ve attached come from the compilation of the solver only. Before that, I ran the packages, parameters and functions needed separately.

---

<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: [May 17, 2019, 4:42pm UTC](https://discourse.julialang.org/t/differentialequations-solve-speed/24329/4 "2019-05-17T16:42:32Z")

</div>

> [@dg.aragones](#):
>
> Before that, I ran the packages, parameters and functions needed separately.

There is a compilation which happens with each new ODE derivative function. We have a prototype way to get rid of that in the near future for the standard Float64 case:

> <https://github.com/SciML/DiffEqBase.jl/pull/208>
>
> \`\`\`julia
> using OrdinaryDiffEq
> 
> \# First time 
> 
> function lorenz(du,u,p,t)
> …du\[1\] = 10.0(u\[2\]-u\[1\])
> du\[2\] = u\[1\]\*(28.0-u\[3\]) - u\[2\]
> du\[3\] = u\[1\]\*u\[2\] - (8/3)\*u\[3\]
> end
> u0 = \[1.0;0.0;0.0\]
> tspan = (0.0,100.0)
> prob = ODEProblem{true,false}(lorenz,u0,tspan)
> @time sol = solve(prob,Tsit5())
> 
> \#6.820159 seconds (16.06 M allocations: 800.508 MiB, 8.44% gc time)
> 
> \# Enable No-Recompile Mode
> 
> function lorenz2(du,u,p,t)
> du\[1\] = 10.0(u\[2\]-u\[1\])
> du\[2\] = u\[1\]\*(28.0-u\[3\]) - u\[2\]
> du\[3\] = u\[1\]\*u\[2\] - (8/3)\*u\[3\]
> end
> prob = ODEProblem{true,false}(lorenz2,u0,tspan)
> @time sol = solve(prob,Tsit5())
> 
> \#0.004248 seconds (12.96 k allocations: 1.375 MiB)
> 
> using BenchmarkTools
> 
> @btime sol = solve(prob,Tsit5())
> 
> \# 1.146 ms (12961 allocations: 1.37 MiB)
> 
> \# Disable No-Recompile Mode
> 
> function lorenz3(du,u,p,t)
> du\[1\] = 10.0(u\[2\]-u\[1\])
> du\[2\] = u\[1\]\*(28.0-u\[3\]) - u\[2\]
> du\[3\] = u\[1\]\*u\[2\] - (8/3)\*u\[3\]
> end
> prob = ODEProblem{true}(lorenz3,u0,tspan)
> @time sol = solve(prob,Tsit5())
> 
> \#1.839436 seconds (3.97 M allocations: 190.402 MiB, 4.78% gc time)
> 
> @btime sol = solve(prob,Tsit5())
> 
> \# 1.078 ms (12963 allocations: 1.37 MiB)
> \`\`\`
> 
> @YingboMa . 
> 
> Note that this isn't safe to use without \`remake\` making sure types in the FunctionWrapper change whenever changing the types, which is important when doing things like AD. But this is what would be required for static compilation to be done completely.
