# LinearAlgebra.inv matrix inversion slower than MATLAB?

**URL:** <https://discourse.julialang.org/t/linearalgebra-inv-matrix-inversion-slower-than-matlab/70066>\
**Category:** Performance\
**Tags:** performance\
**Created:** [October 19, 2021, 9:05pm UTC](https://discourse.julialang.org/t/linearalgebra-inv-matrix-inversion-slower-than-matlab/70066 "2021-10-19T21:05:04Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Abhijit\_Chowdhary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abhijit_chowdhary/32/29227_2.png) [@Abhijit\_Chowdhary](https://discourse.julialang.org/u/Abhijit_Chowdhary)\
**Post date:** [October 24, 2021, 8:10pm UTC](https://discourse.julialang.org/t/linearalgebra-inv-matrix-inversion-slower-than-matlab/70066/22 "2021-10-24T20:10:03Z")

</div>

Hmm, as a reference, this is my performance numbers on a Ryzen 3700X:

```julia
MATLAB R2021b
>> A = rand(4000); f = @() inv(A); timeit(f)
ans =

    0.5925
>> version -blas                   
ans =

    'Intel(R) Math Kernel Library Version 2019.0.3 Product Build 20190125 for Intel(R) 64 architecture applications, CNR branch auto'

```

Julia with OpenBLAS

```julia
julia> VERSION
v"1.7.0-rc1"
julia> using LinearAlgebra, BenchmarkTools
julia> A = rand(4000,4000);
julia> @btime inv($A);
  753.273 ms (6 allocations: 124.05 MiB)
julia> LinearAlgebra.versioninfo()
BLAS: libblastrampoline (f2c_capable)
 --> /home/trostaft/Documents/AcademicFiles/Software/julia-1.7.0-rc1/bin/../lib/julia/libopenblas64_.so (ILP64)

```

Julia with MKL

```julia
julia> VERSION
v"1.7.0-rc1"
julia> using LinearAlgebra, BenchmarkTools
julia> using MKL
julia> A = rand(4000,4000);
julia> @btime inv($A);
  688.735 ms (6 allocations: 124.05 MiB)
julia> LinearAlgebra.versioninfo()
BLAS: libblastrampoline (f2c_capable)
 --> /home/trostaft/.julia/artifacts/72d4adc3ef9236a92f4fefeb0291cb6e8aaae2d7/lib/libmkl_rt.so (ILP64)

```

So, despite the fact that I’m on an AMD CPU, MKL still results in a performance improvement for `inv` over OpenBLAS. Upping the size of the matrix, the performance gain from OpenBLAS and MKL is still around 13%.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [October 25, 2021, 7:04am UTC](https://discourse.julialang.org/t/linearalgebra-inv-matrix-inversion-slower-than-matlab/70066/23 "2021-10-25T07:04:12Z")

</div>

I think MathWorks hasn’t updated the MKL version in MATLAB in order to keep using the _hack_ to run optimized code paths on AMD CPU’s.

Yet, on the latest versions of MKL Intel has created a dedicated code path to AMD Ryzen.  
Though not as optimized as using the Haswell / Skylake code path but still better than OpenBLAS.  
The issue with latest MKL is the hack was disabled (Hence MATLAB uses old version).

[Previous page](https://discourse.julialang.org/t/linearalgebra-inv-matrix-inversion-slower-than-matlab/70066.md?page=1)
