SciML/OrdinaryDiffEq.jl

Avoid allocations for interpolator evaluation

Open

#1,270 opened on Sep 15, 2020

View on GitHub
 (6 comments) (0 reactions) (0 assignees)Julia (262 forks)batch import
help wantedperformance

Repository metrics

Stars
 (656 stars)
PR merge metrics
 (Avg merge 3d 12h) (74 merged PRs in 30d)

Description

Hi,

I have noticed that the interpolation of a solution allocates. This is a MWE:

using OrdinaryDiffEq
using BenchmarkTools

function f1(du, u, p, t)
  du[1] = u[1]
end
prob1 = ODEProblem{true}(f1, ones(1), (0.0, 1.0), nothing)
sol1 = solve(prob1, Tsit5())

f2(u, p, t) = u
prob2 = ODEProblem{false}(f2, 1.0, (0.0, 1.0), nothing)
sol2 = solve(prob2, Tsit5())

out = zeros(1)

Then:

julia> @btime $sol1(0.1)
  1.181 μs (18 allocations: 416 bytes)
1-element Array{Float64,1}:
 1.1051709180988993

julia> @btime $sol1($out, 0.1)
  1.143 μs (17 allocations: 320 bytes)
1-element Array{Float64,1}:
 1.1051709180988993

julia> @btime $sol2(0.1)
  1.267 μs (25 allocations: 1.33 KiB)
1.1051709180988993

julia> @btime $sol2($out, 0.1)
  1.185 μs (24 allocations: 1.31 KiB)
1-element Array{Float64,1}:
 1.1051709180988993

Thanks!

Contributor guide