# Gaussian Elimination Function that is Fast

**URL:** <https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714>\
**Category:** Performance\
**Tags:** linearalgebra\
**Created:** [July 24, 2022, 1:37pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714 "2022-07-24T13:37:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 24, 2022, 1:37pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714/1 "2022-07-24T13:37:08Z")

</div>

I was testing this package from this repository, it is faster than `A \ b`

I do not know any other competitor for calculating Gaussian Elimination that can be measured with @time. If anyone knows, I would like to know and do benchmark test on all of them.

This is from Julia REPL and JupyterNotebook:

 ![Capture d’écran_2022-07-24_20-26-47](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc9e33a2bc57ad16a6cd994f817de9a5f78c14b6.png)  
 ![Capture d’écran_2022-07-24_20-34-39](https://global.discourse-cdn.com/julialang/original/3X/0/e/0e5563139cc983d6475a945f45ea660cd75e053b.png)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [July 24, 2022, 1:48pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714/2 "2022-07-24T13:48:59Z")

</div>

A couple of suggestions: do not test with `@time` in the global scope. Use `BenchmarkTools` to test performance.  
For instance

```julia
julia> b = rand(3)
3-element Vector{Float64}:
 8.30429e-01
 1.46162e-01
 8.75693e-02

julia> A = rand(3, 3)
3×3 Matrix{Float64}:
 2.81443e-01 1.72279e-01 4.03091e-01
 2.49288e-01 2.35197e-01 2.02405e-01
 2.82910e-01 8.54934e-01 9.42708e-01

julia> using BenchmarkTools

julia> @btime $A \ $b
  563.934 ns (3 allocations: 288 bytes)
3-element Vector{Float64}:
  1.46582e+00
 -2.88576e+00
  2.27007e+00

julia> 

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 24, 2022, 2:17pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714/3 "2022-07-24T14:17:12Z")

</div>

> [@PetrKryslUCSD](#):
>
> `using BenchmarkTools`

Great suggestion, what is the different with this BenchmarkTools?

The result is the other way around now.

 ![Capture d’écran_2022-07-24_21-14-14](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d9e945aaf390c1268fab4843d32dff5b1242942.png)

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 24, 2022, 4:12pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714/4 "2022-07-24T16:12:51Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> what is the different with this

You should take a look at the BenchmarkTools documentation, where it is [explained](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables) why you should not use global variables when benchmarking. Note the dollar signs in @PetrKryslUCSD’s code. Also, it is nicer to [provide example code and outputs in code blocks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) rather than using screen images. That way other people can copy and paste your examples.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [July 24, 2022, 4:19pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714/5 "2022-07-24T16:19:27Z")

</div>

A couple more packages for you to look at: [MKL](https://github.com/JuliaLinearAlgebra/MKL.jl) and [RecursiveFactorization](https://github.com/JuliaLinearAlgebra/RecursiveFactorization.jl).

---

<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:** [July 24, 2022, 4:54pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714/6 "2022-07-24T16:54:46Z")

</div>

LAPACK, OpenBLAS, MKL etc are all optimized for large matrices. For a tiny matrix like 3x3 they introduce a lot of overhead and a simple loop will often be faster.

But if you have lots of tiny 3x3 systems it will be faster still to use StaticArrays, which can unroll and inline everything.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 25, 2022, 12:14pm UTC](https://discourse.julialang.org/t/gaussian-elimination-function-that-is-fast/84714/7 "2022-07-25T12:14:28Z")

</div>

I try BenchmarkTools on finding a solution of a linear system and compare the time for two different methods in Julia:

```julia
A = [1 2 3;
     2 5 3;
     1 0 8]

b = [5; 3; 17]

```

```julia
using BenchmarkTools

@btime $A \ $b

```

```julia
@btime inv(A)*b

```

![Capture d’écran_2022-07-25_19-14-16](https://global.discourse-cdn.com/julialang/original/3X/e/6/e6cff2426c543345609da22aedad5ffa502005a5.png)
