# Performance tips for differential equation RHS

**URL:** <https://discourse.julialang.org/t/performance-tips-for-differential-equation-rhs/108934>\
**Category:** New to Julia\
**Tags:** question, package, optimization\
**Created:** [January 18, 2024, 12:19am UTC](https://discourse.julialang.org/t/performance-tips-for-differential-equation-rhs/108934 "2024-01-18T00:19:58Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 21, 2024, 6:58pm UTC](https://discourse.julialang.org/t/performance-tips-for-differential-equation-rhs/108934/21 "2024-01-21T18:58:12Z")

</div>

It’s difficult to keep track of what your current code looks like, but if you still have this part there

> [@madeline](#):
>
> `N = length(coupling_matrix[1, :])`

you should fix it. This allocates and copies an entire vector, just to read its length. That’s very wasteful. Instead, just use `size` which has virtually zero cost:

```julia
N = size(coupling_matrix, 2)

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [January 22, 2024, 4:57am UTC](https://discourse.julialang.org/t/performance-tips-for-differential-equation-rhs/108934/22 "2024-01-22T04:57:40Z")

</div>

Something is seriously wrong there. Are you on Julia 1.10? It added some type instabilities.

> <https://github.com/JuliaSIMD/LoopVectorization.jl/issues/526>
>
> Apparently, multiplying a vector of floats with a vector of ints causes problems… with the julia compiler. The return type \`res\` cannot be inferred anymore (\`red::Any\`) and this causes performance losses.
> I have tested this on Julia 1.9.3, and there the example below works fine there, but not on Julia 1.10.
> Of course, one can manually promote Integers to Floats before multiplying, at least as a workaround.
> 
> Minimum working example:
> \`\`\`
> using LoopVectorization
> function LVTest(a1,a2)
> res = zero(eltype(a1))
> @turbo for i in eachindex(a1,a2)
> res += a1\[i\]\*a2\[i\]
> end
> return res
> end
> 
> aFloat = zeros(10)
> aInt = zeros(Int,10)
> 
> @code\_warntype LVTest(aFloat,aInt) #prints type Any for res on Julia 1.10 and does not show any type instabilities for 1.9
> 
> function checkAllocs()
> aFloat = zeros(10)
> aInt = zeros(Int,10)
> LVTest(aFloat,aInt) # compile
> println("Allocations: ",@allocated LVTest(aFloat,aInt))
> end
> 
> checkAllocs() # prints 0 on Julia 1.9 but 2304 on Julia 1.10
> \`\`\`
> 
> The output of \`versioninfo()\`:
> \`\`\`
> Julia Version 1.9.3
> Commit bed2cd540a1 (2023-08-24 14:43 UTC)
> Build Info:
> Official https://julialang.org/ release
> Platform Info:
> OS: Linux (x86\_64-linux-gnu)
> CPU: 8 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
> WORD\_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-14.0.6 (ORCJIT, haswell)
> Threads: 1 on 8 virtual cores
> Environment:
> JULIA\_PKG\_USE\_CLI\_GIT = true
> JULIA\_DEPOT\_PATH = /storage/niggeni/.julia\_hexagon
> JULIA\_IMAGE\_THREADS = 1
> \`\`\`
> 
> \`\`\`
> Julia Version 1.10.0
> Commit 3120989f39b (2023-12-25 18:01 UTC)
> Build Info:
> Official https://julialang.org/ release
> Platform Info:
> OS: Linux (x86\_64-linux-gnu)
> CPU: 8 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
> WORD\_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-15.0.7 (ORCJIT, haswell)
> Threads: 1 on 8 virtual cores
> Environment:
> JULIA\_PKG\_USE\_CLI\_GIT = true
> JULIA\_DEPOT\_PATH = /storage/niggeni/.julia\_hexagon
> \`\`\`

---

<div class="post-metadata">

**Author:** ![madeline](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/madeline/32/46618_2.png) [@madeline](https://discourse.julialang.org/u/madeline)\
**Post date:** [January 23, 2024, 5:11am UTC](https://discourse.julialang.org/t/performance-tips-for-differential-equation-rhs/108934/23 "2024-01-23T05:11:55Z")

</div>

You’re right. Declaring the elements of coupling\_matrix as floats and introducing loop vectorization just helped a ton. I don’t have access to the same PC as before, but it just sped up about 4x. Thank you!

[Previous page](https://discourse.julialang.org/t/performance-tips-for-differential-equation-rhs/108934.md?page=1)
