# Help speeding up \`DiscreteProblem\`

**URL:** <https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986>\
**Category:** Modelling & Simulations\
**Created:** [July 23, 2025, 9:01pm UTC](https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986 "2025-07-23T21:01:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [July 23, 2025, 9:01pm UTC](https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986/1 "2025-07-23T21:01:20Z")

</div>

Hi Everyone,

I’m finding that using `DiscreteProblem` on my toy example is about 4x slower than a vanilla loop with pre-allocated output.

[`DiscreteProblem` version](https://github.com/epirecipes/sir-julia/blob/master/markdown/function_map/function_map.md)  
[Base Julia version](https://github.com/epirecipes/sir-julia/blob/master/markdown/function_map_vanilla/function_map_vanilla.md)

My model is as follows:

```Julia
function sir_map!(du,u,p,t)
    (S,I,R) = u
    (β,c,γ,δt) = p
    N = S+I+R
    infection = rate_to_proportion(β*c*I/N,δt)*S
    recovery = rate_to_proportion(γ,δt)*I
    @inbounds begin
        du[1] = S-infection
        du[2] = I+infection-recovery
        du[3] = R+recovery
    end
    nothing
end;

```

And my base code loop for solving it is as follows:

```Julia
function solve_map(f, u0, nsteps, p)
    # Pre-allocate array with correct type
    sol = similar(u0, length(u0), nsteps + 1)
    # Initialize the first column with the initial state
    sol[:, 1] = u0
    # Iterate over the time steps
    @inbounds for t in 2:nsteps+1
        u = @view sol[:, t-1] # Get the current state
        du = @view sol[:, t] # Prepare the next state
        f(du, u, p, t) # Call the function to update du
    end
    return sol
end;

```

I solve the above using `DiscreteProblem` as follows.

```Julia
prob_map = DiscreteProblem(sir_map!,u0,tspan,p)
sol_map = solve(prob_map,FunctionMap());

```

I suspect that the time difference between the implementations is due to not pre-allocating the output for the solution of `DiscreteProblem`; any suggestions on how to speed it up?

---

<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:** [July 25, 2025, 4:08am UTC](https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986/2 "2025-07-25T04:08:47Z")

</div>

Try SimpleDiffEq.jl `SimpleFunctionMap`. Using the full ODE integrator with all of its other error checks can get in the way if there’s no continuous element.

---

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [July 25, 2025, 1:40pm UTC](https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986/3 "2025-07-25T13:40:31Z")

</div>

Thanks @ChrisRackauckas! Using `SimpleFunctionMap` gets me nearly all the way there:

```Julia
julia> @benchmark solve(prob_map,FunctionMap())
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min … max): 38.250 μs … 14.435 ms ┊ GC (min … max): 0.00% … 99.47%
 Time (median): 42.917 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 46.853 μs ± 192.679 μs ┊ GC (mean ± σ): 6.77% ± 1.72%

    ▅▇▇▄▇▄▄▃▆█▆▆▆▆▃▅▂▂▁                                         
  ▂▅█████████████████████▆▇▆▅▄▄▃▃▃▃▂▂▂▂▂▂▂▂▂▂▁▁▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁ ▄
  38.2 μs Histogram: frequency by time 59.4 μs <

```

```julia
julia> @benchmark solve(prob_map,SimpleFunctionMap())
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min … max): 14.916 μs … 15.643 ms ┊ GC (min … max): 0.00% … 99.67%
 Time (median): 16.709 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 18.751 μs ± 156.266 μs ┊ GC (mean ± σ): 8.31% ± 1.00%

       ▅█▇▂▂▁ ▁                                                
  ▁▂▃█████████▆███▇▅▆▅▅▄▅▄▄▂▃▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▃
  14.9 μs Histogram: frequency by time 24.3 μs <

```

```julia
julia> @benchmark solve_map(sir_map!, u0, nsteps, p)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min … max): 11.625 μs … 47.708 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 12.500 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 12.690 μs ± 1.352 μs ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▄█ █ ▃▇▇                                                  
  ██▇▄██▆▅███▇▆▆▆▅▆▄▄▄▄▆▃▃▃▃▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▃
  11.6 μs Histogram: frequency by time 17 μs <

```

---

<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:** [July 25, 2025, 1:58pm UTC](https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986/4 "2025-07-25T13:58:18Z")

</div>

Open an issue for the last bit. We should optimize that @Oscar_Smith

---

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [July 25, 2025, 2:20pm UTC](https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986/5 "2025-07-25T14:20:55Z")

</div>

In `SimpleDiffEq.jl`?

---

<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:** [July 25, 2025, 2:48pm UTC](https://discourse.julialang.org/t/help-speeding-up-discreteproblem/130986/6 "2025-07-25T14:48:49Z")

</div>

yes please
