# Julia versus MATLAB

**URL:** <https://discourse.julialang.org/t/julia-versus-matlab/79360>\
**Category:** Performance\
**Tags:** matlab, linearalgebra\
**Created:** [April 12, 2022, 1:39am UTC](https://discourse.julialang.org/t/julia-versus-matlab/79360 "2022-04-12T01:39:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [April 12, 2022, 1:39am UTC](https://discourse.julialang.org/t/julia-versus-matlab/79360/1 "2022-04-12T01:39:09Z")

</div>

I am trying Julia 1.7.2 on an M1 MacBook Air for bench marking Eigen against MATLAB 2022a

```julia
a=randn(100,100)
tic, for x=1:100; eig(a); end; toc
Elapsed time is 0.295766 seconds.

```

whereas Julia v1.7.2

```julia
a=randn(Float64, (100,100))
 @time begin
       for x in 1:100
       eigen(a);
       end
       end
  1.691869 seconds (2.10 k allocations: 41.257 MiB, 1.18% gc time)

```

Both are in x86 mode, I believe. Is there any reason why Julia should be so slow?  
Kumar

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 12, 2022, 1:48am UTC](https://discourse.julialang.org/t/julia-versus-matlab/79360/2 "2022-04-12T01:48:54Z")

</div>

How many threads is BLAS using? These are both just calling out to BLAS, so with some parameter tuning, they should be identical.

---

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [April 12, 2022, 1:56am UTC](https://discourse.julialang.org/t/julia-versus-matlab/79360/3 "2022-04-12T01:56:06Z")

</div>

BLAS is using 8 threads. I changed it to 16 with no change. In fact, I changed it to 1 thread and the executed time was the same.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 12, 2022, 2:02am UTC](https://discourse.julialang.org/t/julia-versus-matlab/79360/4 "2022-04-12T02:02:12Z")

</div>

In my computer `MKL`’s eigen is 4x as fast as the default, so maybe this is related (I think MATLAB uses MKL by default).

using BLAS:

```julia
julia> @time begin
              for x in 1:100
              eigen(a);
              end
              end
  1.968685 seconds (2.10 k allocations: 41.257 MiB, 5.29% gc time)

```

with MKL:

```julia
julia> @time begin
              for x in 1:100
              eigen(a);
              end
              end
  0.519582 seconds (2.10 k allocations: 60.788 MiB, 0.39% gc time)

```

(second execution in both cases, to discount compilation)

---

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [April 12, 2022, 2:10am UTC](https://discourse.julialang.org/t/julia-versus-matlab/79360/5 "2022-04-12T02:10:18Z")

</div>

I don’t expect the MacBook Air to benefit from MKL. The processor is ARM-based. But x86 has an emulation mode.  
/K

---

<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:** [April 12, 2022, 2:12am UTC](https://discourse.julialang.org/t/julia-versus-matlab/79360/6 "2022-04-12T02:12:56Z")

</div>

See this comment from an earlier discussion:

> [@Julia is slower than MATLAB at diagonalizing matrices](https://discourse.julialang.org/t/julia-is-slower-than-matlab-at-diagonalizing-matrices/78174/7):
>
> I guess that with one or no output argument Matlab’s [eig](https://www.mathworks.com/help/matlab/ref/eig.html) just computes the eigenvalues and does not particularly compute/store the eigenvectors. In contrast, in Julia the [eigen](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.eigen) computes full eigendecomposition, that is, both eigenvalues and eigenvectors. If only eigenvalues are required, use [eigvals](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.eigvals) instead. The performance of Matlab and Julia are then pretty much identical on my old laptop (running Linux). In fact, Julia even a bit faster. In Matlab 2022a (and yes, I did run the code a few tim…
