# Large difference in computation time of expv from 4\*4 matrix to 3\*3 matrix

**URL:** <https://discourse.julialang.org/t/large-difference-in-computation-time-of-expv-from-4-4-matrix-to-3-3-matrix/133376>\
**Category:** General Usage\
**Tags:** question, package, performance, staticarrays\
**Created:** [October 23, 2025, 8:50am UTC](https://discourse.julialang.org/t/large-difference-in-computation-time-of-expv-from-4-4-matrix-to-3-3-matrix/133376 "2025-10-23T08:50:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xu\_Shan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xu_shan/32/214900_2.png) [@Xu\_Shan](https://discourse.julialang.org/u/Xu_Shan)\
**Post date:** [October 23, 2025, 8:50am UTC](https://discourse.julialang.org/t/large-difference-in-computation-time-of-expv-from-4-4-matrix-to-3-3-matrix/133376/1 "2025-10-23T08:50:00Z")

</div>

Hi Guys,

I tried to profile the performance of `ExponentialUtilities` on static array as matrix. I found there is a big jump from 3_3 matrix to 4_4 matrix. May I know why? Here is the script I used for the testing:

```julia-auto
using ExponentialUtilities
using BenchmarkTools
using StaticArrays

t = 1e-3
# check for A from 2*2 to 10*10 case
for n in 2:10
    An = rand(n, n)
    Asn = SMatrix{n, n}(An)
    vn = rand(n)
    vsn = SVector{n}(vn)
    println("n = $n")
    @btime expv($t, $An, $vn)
    @btime expv($t, $Asn, $vsn)
    @btime exp($t*$Asn)*$vsn
    println("===================================")
end

```

results are

```julia-auto
n = 2
  865.206 ns (27 allocations: 1.39 KiB)
  13.360 ns (0 allocations: 0 bytes)
  13.346 ns (0 allocations: 0 bytes)
===================================
n = 3
  1.212 μs (27 allocations: 1.67 KiB)
  29.480 ns (0 allocations: 0 bytes)
  29.565 ns (0 allocations: 0 bytes)
===================================
n = 4
  2.120 μs (27 allocations: 2.22 KiB)
  145.361 ns (0 allocations: 0 bytes)
  135.082 ns (0 allocations: 0 bytes)
===================================
n = 5
  2.477 μs (27 allocations: 2.75 KiB)
  188.325 ns (0 allocations: 0 bytes)
  228.850 ns (0 allocations: 0 bytes)
===================================
n = 6
  3.469 μs (27 allocations: 3.64 KiB)
  203.497 ns (0 allocations: 0 bytes)
  344.448 ns (0 allocations: 0 bytes)
===================================
n = 7
  5.000 μs (27 allocations: 4.55 KiB)
  270.764 ns (0 allocations: 0 bytes)
  533.116 ns (0 allocations: 0 bytes)
===================================
n = 8
  4.986 μs (27 allocations: 5.41 KiB)
  298.832 ns (0 allocations: 0 bytes)
  815.551 ns (0 allocations: 0 bytes)
===================================
n = 9
  6.917 μs (27 allocations: 6.53 KiB)
  315.094 ns (0 allocations: 0 bytes)
  1.054 μs (0 allocations: 0 bytes)
===================================
n = 10
  7.875 μs (27 allocations: 8.27 KiB)
  347.028 ns (0 allocations: 0 bytes)
  1.375 μs (0 allocations: 0 bytes)
===================================

```

You could see there is a big jump from the case of `n=2` to `n=3`. May I ask the reason? because I am using it in my model to calculate the exponential of a 4\*4 matrix. I’m thinking reduce the size of the matrix, then I found there is a big difference between the case of 2 and 3…nearly 4-5 bigger… but why?

---

<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:** [October 23, 2025, 9:12am UTC](https://discourse.julialang.org/t/large-difference-in-computation-time-of-expv-from-4-4-matrix-to-3-3-matrix/133376/2 "2025-10-23T09:12:41Z")

</div>

Could just be the conditioning of the matrix.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [October 23, 2025, 9:14am UTC](https://discourse.julialang.org/t/large-difference-in-computation-time-of-expv-from-4-4-matrix-to-3-3-matrix/133376/3 "2025-10-23T09:14:51Z")

</div>

I don’t know a lot about linear algebra but I do know that most things take time O(n^3) (unless you have large matrices which can often use cleverer algorithms). So if you double n then you expect the time to go up by 8x, which appears to roughly hold in your timings.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 23, 2025, 12:13pm UTC](https://discourse.julialang.org/t/large-difference-in-computation-time-of-expv-from-4-4-matrix-to-3-3-matrix/133376/4 "2025-10-23T12:13:20Z")

</div>

The matrix exponential uses a polyalgorithm that switches methods based on various things. e.g. `expv` for StaticArrays [switches to a different method](https://github.com/SciML/ExponentialUtilities.jl/blob/d4e4c7bf487593c7672b7f88110fab54ecccd97f/ext/ExponentialUtilitiesStaticArraysExt.jl#L119) for n \times n matrices when n \> 5. For n = 3 and n = 4, it looks like it just calls [this `exp` method from StaticArrays](https://github.com/SciML/ExponentialUtilities.jl/blob/d4e4c7bf487593c7672b7f88110fab54ecccd97f/ext/ExponentialUtilitiesStaticArraysExt.jl#L119), but even that method switches algorithms depending on the norm of the matrix.

Every time you cross the boundaries of one of these switches, there is going to be a jump in cost (in addition to the general O(n^3) trend).
