# Significant Difference in Runtime Between Specified and Unspecified Noise

**URL:** <https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745>\
**Category:** New to Julia\
**Tags:** question, differentialequation\
**Created:** [June 17, 2024, 10:29am UTC](https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745 "2024-06-17T10:29:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sepanta-Mssvn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepanta-mssvn/32/209502_2.png) [@Sepanta-Mssvn](https://discourse.julialang.org/u/Sepanta-Mssvn)\
**Post date:** [June 17, 2024, 10:29am UTC](https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745/1 "2024-06-17T10:29:55Z")

</div>

There seems to be a significant difference in runtime when the noise process is specified. In the simple case of:

```julia
using DifferentialEquations
using BenchmarkTools

global T = 10.0::Float64

f(du, u, p, t) = du .= 0.0+0.0im
g(du, u, p, t) = du .= 1.0+0.0im

prob = SDEProblem(f, g, zeros(ComplexF64,5), (0.0, T))

function func(prob)
    return solve(prob, EM(), dt = 1e-4, saveat = 1e-3)
end

```

We have a 5D stochastic function that only depends on the noise term. Benchmarking the function, we see that:

```julia
@benchmark func(p) setup=(p=$prob) seconds=20.0 samples=20000 evals=1
BenchmarkTools.Trial: 1568 samples with 1 evaluation.
 Range (min … max): 12.135 ms … 18.741 ms ┊ GC (min … max): 0.00% … 31.25%
 Time (median): 12.658 ms ┊ GC (median): 0.00%
 Time (mean ± σ): 12.727 ms ± 378.553 μs ┊ GC (mean ± σ): 0.35% ± 1.73%

```

Then, changing the code by adding the following line and changing the defined SDE problem via:

```julia
W = WienerProcess(0.0,zeros(ComplexF64,5))
prob = SDEProblem(f, g, zeros(ComplexF64,5), (0.0, T), noise = W)

```

My understanding is that the Wiener process defined here ought to be the same as the automatically selected one. (I have checked and the automatically selected noise is complex as well.) However, benchmarking this function gives:

```julia
@benchmark func(p) setup=(p=$prob) seconds=20.0 samples=20000 evals=1
BenchmarkTools.Trial: 148 samples with 1 evaluation.
 Range (min … max): 61.358 ms … 350.173 ms ┊ GC (min … max): 15.66% … 82.36%
 Time (median): 84.430 ms ┊ GC (median): 42.11%
 Time (mean ± σ): 134.533 ms ± 95.967 ms ┊ GC (mean ± σ): 60.11% ± 22.32%

```

Here, the runtime is more than 10 times longer. As such, I was wondering if anyone had any insight into where this difference in runtime comes from, and if it is possible to bring specified noise processes in line with automatically selected noises.

---

<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 17, 2024, 11:18am UTC](https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745/2 "2024-06-17T11:18:30Z")

</div>

By default it uses SimpleWienerProcess that optimizes a bit more.

---

<div class="post-metadata">

**Author:** ![Sepanta-Mssvn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepanta-mssvn/32/209502_2.png) [@Sepanta-Mssvn](https://discourse.julialang.org/u/Sepanta-Mssvn)\
**Post date:** [June 17, 2024, 11:59am UTC](https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745/3 "2024-06-17T11:59:43Z")

</div>

Changing the noise type to

```julia
W = SimpleWienerProcess!(0.0,zeros(ComplexF64,5))

```

gives:

```julia
@benchmark func(p) setup=(p=$prob) seconds=20.0 samples=20000 evals=1
BenchmarkTools.Trial: 36 samples with 1 evaluation.
 Range (min … max): 37.388 ms … 1.656 s ┊ GC (min … max): 0.00% … 75.49%
 Time (median): 429.430 ms ┊ GC (median): 42.14%
 Time (mean ± σ): 547.136 ms ± 428.562 ms ┊ GC (mean ± σ): 52.77% ± 24.12%

```

which is worse than the WienerProcess. Meanwhile

```julia
W = SimpleWienerProcess(0.0,zeros(ComplexF64,5))

```

throws the following error:

```julia
ERROR: MethodError: no method matching WHITE_NOISE_DIST(::SimpleNoiseProcess{…}, ::Float64, ::Vector{…}, ::SciMLBase.NullParameters, ::Float64, ::RandomNumbers.Xorshifts.Xoroshiro128Plus)
An arithmetic operation was performed on a NullParameters object. This means no parameters were passed into the AbstractSciMLProblem (e.x.: ODEProblem) but the parameters object `p` was used in an arithmetic expression.

```

On the other hand,

```julia
WienerProcess!(0.0,zeros(ComplexF64,5))

```

gives:

```julia
@BenchmarkTools.Trial: 527 samples with 1 evaluation.
 Range (min … max): 23.701 ms … 121.852 ms ┊ GC (min … max): 0.00% … 72.00%
 Time (median): 33.316 ms ┊ GC (median): 0.00%
 Time (mean ± σ): 37.857 ms ± 18.365 ms ┊ GC (mean ± σ): 12.99% ± 17.70%

```

Unfortunately I don’t understand why these are the case.

---

<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 17, 2024, 12:13pm UTC](https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745/4 "2024-06-17T12:13:54Z")

</div>

In-place will reuse memory and thus limit the number of allocation operations.

---

<div class="post-metadata">

**Author:** ![Sepanta-Mssvn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sepanta-mssvn/32/209502_2.png) [@Sepanta-Mssvn](https://discourse.julialang.org/u/Sepanta-Mssvn)\
**Post date:** [June 17, 2024, 12:30pm UTC](https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745/5 "2024-06-17T12:30:19Z")

</div>

I see, thank you. In that case:

1. Why does SimpleWienerProcess! take longer than WienerProcess! (and WienerProcess)?
2. Is there a reason not to use in-place?
3. Back to the original question, in all cases a specified noise was slower than the automatic one. Is there a way of improving the performance of specified noises?

I appreciate your patience btw, I’m just trying to understand all of this.

---

<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 5, 2024, 1:41pm UTC](https://discourse.julialang.org/t/significant-difference-in-runtime-between-specified-and-unspecified-noise/115745/6 "2024-07-05T13:41:13Z")

</div>

> [@Sepanta-Mssvn](#):
>
> Is there a reason not to use in-place?

StaticArrays

> [@Sepanta-Mssvn](#):
>
> - Why does SimpleWienerProcess! take longer than WienerProcess! (and WienerProcess)?

I’d have to investigate that deeper.

> [@Sepanta-Mssvn](#):
>
> Back to the original question, in all cases a specified noise was slower than the automatic one. Is there a way of improving the performance of specified noises?

If you share a profile I could help point out what can be done.
