# SVD 2x slower than in Matlab and how to get best performance on Windows10

**URL:** <https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645>\
**Category:** Performance\
**Created:** [March 8, 2019, 6:21pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645 "2019-03-08T18:21:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baicai\_Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baicai_xiao/32/7395_2.png) [@Baicai\_Xiao](https://discourse.julialang.org/u/Baicai_Xiao)\
**Post date:** [March 8, 2019, 6:21pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/1 "2019-03-08T18:21:05Z")

</div>

Hello ,

Recently, I found that julia svd is slower than the one in matlab. It has been discussed once here in [https://github.com/JuliaLang/julia/issues/3521](https://github.com/JuliaLang/julia/issues/3521), but after reading this, I am still not quite sure what is exactly happening here. Could someone please help me? Thanks in advance.

The code is

```julia
A = rand(1000,1000);
@benchmark svd(A)

```

output is

```julia
BenchmarkTools.Trial:
  memory estimate: 45.90 MiB
  allocs estimate: 13
  --------------
  minimum time: 449.142 ms (0.90% GC)
  median time: 580.609 ms (3.14% GC)
  mean time: 597.990 ms (4.71% GC)
  maximum time: 784.079 ms (2.63% GC)
  --------------
  samples: 9
  evals/sample: 1

```

and for Matlab is

```julia
tic;svd(A);toc

```

output is

```julia
Elapsed time is 0.286179 seconds.

```

I am using Windows10 system, julia is the binary downloaded from the website. It seems the lapack version for julia is libopenblas64\_, and mkl for matlab. Could this be the reasons?

Is anyone able to intall julia on Windows10 so that it has equivalent performance as matlab? If so, how did you do that? Thanks very much.

Sincerely  
BaiCai

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 8, 2019, 6:50pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/2 "2019-03-08T18:50:43Z")

</div>

> [@Baicai\_Xiao](#):
>
> It seems the lapack version for julia is libopenblas64\_, and mkl for matlab. Could this be the reasons?

It is likely _the_ reason.

You can build julia with MKL but it is a bit tricky. Check out [GitHub - JuliaLinearAlgebra/MKL.jl: Intel MKL linear algebra backend for Julia](https://github.com/JuliaComputing/MKL.jl) (but read the caveats).

---

<div class="post-metadata">

**Author:** ![Baicai\_Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baicai_xiao/32/7395_2.png) [@Baicai\_Xiao](https://discourse.julialang.org/u/Baicai_Xiao)\
**Post date:** [March 8, 2019, 7:47pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/4 "2019-03-08T19:47:34Z")

</div>

It’s a little bit embarrassing, but I don’t know how to install it. There is no instructions on how to install it. And since I already have mkl installed on my computer, reinstall mkl is not necessary. Thanks anyway, i would try to figure it out.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 8, 2019, 10:12pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/5 "2019-03-08T22:12:30Z")

</div>

It’s very difficult to build with MKL, on a Mac at least. I do it, but it requires

1. Installing MKL
2. Setting Make.user
3. Running make until it errors out
4. Make symbolic links as explained in [https://github.com/JuliaLang/julia/issues/15133](https://github.com/JuliaLang/julia/issues/15133)
5. Rerun make

That said, it’s worth it for the speed up. Although you can’t use PyPlot.jl with MKL so for plotting it helps to keep both versions around.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 9, 2019, 8:52am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/6 "2019-03-09T08:52:15Z")

</div>

For me, on Linux, it is super simple to link against MKL. Just follow the instructions on Julia’s github page.

Also, FWIW, it is possible to have MKL and use PyPlot.jl. See [Segfault with Julia MKL build (library conflict) · Issue #443 · JuliaPy/PyCall.jl · GitHub](https://github.com/JuliaPy/PyCall.jl/issues/443#issuecomment-405632507).

---

<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:** [March 9, 2019, 10:52am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/7 "2019-03-09T10:52:30Z")

</div>

What’s wrong with MKL.jl way of doing it? Is it not Mac compatible?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [March 9, 2019, 10:57am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/8 "2019-03-09T10:57:57Z")

</div>

I haven’t tried MKL.jl, but the warning about slow REPL is not encouraging.

---

<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:** [March 9, 2019, 4:07pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/9 "2019-03-09T16:07:05Z")

</div>

In my opinion, the proper solution for new users who wants MATLAB like performance in Julia is bringing back the MKL Flavor of Julia Pro.

Preferably with all the tweaks of the latest versions of MKL (Handling small matrices, doing same operation many times, etc…) talked about.

In real world it will make a significant difference compared to OpenBLAS used now.

---

<div class="post-metadata">

**Author:** ![Baicai\_Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baicai_xiao/32/7395_2.png) [@Baicai\_Xiao](https://discourse.julialang.org/u/Baicai_Xiao)\
**Post date:** [March 12, 2019, 10:03pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/10 "2019-03-12T22:03:50Z")

</div>

Update:

I did more benchmarks on MKL and OPENBLAS. Also more comparison with matlab. Here are the results.

**MKL:**  
single thread (by setting `BLAS.set_num_threads(1)`), the results are:

```julia
 > A = rand(1000,1000)
 > @benchmark svd(A)
BenchmarkTools.Trial: 
  memory estimate: 45.90 MiB
  allocs estimate: 13
  --------------
  minimum time: 255.843 ms (0.10% GC)
  median time: 275.403 ms (0.09% GC)
  mean time: 270.219 ms (1.18% GC)
  maximum time: 298.626 ms (7.98% GC)
  --------------
  samples: 19
  evals/sample: 1

```

6 threads (`BLAS.set_num_threads(6) `):

```julia
@benchmark svd(A)
BenchmarkTools.Trial: 
  memory estimate: 45.90 MiB
  allocs estimate: 13
  --------------
  minimum time: 108.813 ms (4.38% GC)
  median time: 111.913 ms (0.24% GC)
  mean time: 114.318 ms (1.39% GC)
  maximum time: 154.178 ms (0.23% GC)
  --------------
  samples: 44
  evals/sample: 1

```

**OPENBLAS:**

single thread:

```julia
> A = rand(1000,1000)
>@benchmark svd(A)
BenchmarkTools.Trial: 
  memory estimate: 45.90 MiB
  allocs estimate: 13
  --------------
  minimum time: 322.695 ms (0.19% GC)
  median time: 356.963 ms (1.86% GC)
  mean time: 353.865 ms (3.56% GC)
  maximum time: 385.039 ms (8.32% GC)
  --------------
  samples: 15
  evals/sample: 1

```

6 threads:

```julia
>A = rand(1000,1000)
>@benchmark svd(A)
BenchmarkTools.Trial: 
  memory estimate: 45.90 MiB
  allocs estimate: 13
  --------------
  minimum time: 182.592 ms (3.35% GC)
  median time: 202.389 ms (3.40% GC)
  mean time: 212.733 ms (5.51% GC)
  maximum time: 251.271 ms (3.24% GC)
  --------------
  samples: 24
  evals/sample: 1

```

_ **It seems using MKL is indeed faster than OPENBLAS.** _

However, they are still not comparable with matlab. **for matlab, the results are:**

single thread (by run `matlab singleCompThread `):

```julia
>>A = rand(1000,1000);
>>tic; svd(A);toc
Elapsed time is 0.154609 seconds.

```

6 threads (run matlab directly):

```julia
>> A = rand(1000,1000);
>> tic; svd(A); toc
Elapsed time is 0.074904 seconds.

```

It turns out matlab still works better than Julia +MKL. The tests are done on Ubuntu, and my julia is compiled from source with MKL. I am not quite sure why julia is still slower, I would be super happy if someone give me some hints. Maybe because the way Julia deals with threads?

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 13, 2019, 1:27am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/11 "2019-03-13T01:27:17Z")

</div>

In matlab, `svd` computes only the singular values `S` when called in an expression (`nargout=1`). In julia this is the `svdvals()` function which is much faster. If you want to compare like with like, I think you need `svd(A)` in julia (which computes the “thin” SVD containing U,S,V factors in a factorization object) and `[U,S,V] = SVD(A,'econ')` in matlab.

Edit edit: I thought I should check what matlab does when called without outputs (presumably `nargout=0`). It seems the implicit `ans` variable is involved in a nontrivial way and the user can [set or not set it depending on logic in their function](https://au.mathworks.com/help/matlab/ref/nargout.html#bvj411r-1) 🤦‍♂️. To quote…

> If you check for a `nargout` value of 0 within a function and you specify the value of the output, MATLAB populates `ans` . However, if you check `nargout` and do not specify a value for the output, then MATLAB does not modify `ans` .

---

<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:** [March 13, 2019, 7:19am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/12 "2019-03-13T07:19:51Z")

</div>

> [@c42f](#):
>
> In matlab, `svd` computes only the singular values `S` when called in an expression (`nargout=1`). In julia this is the `svdvals()` function which is much faster. If you want to compare like with like, I think you need `svd(A)` in julia (which computes the “thin” SVD containing U,S,V factors in a factorization object) and `[U,S,V] = SVD(A,'econ')` in matlab.

Indeed on my machine:

```julia
numElements = 1000;

mA = randn(numElements, numElements );
hF = @() svd(mA, 'econ');
hG = @() svd(mA);

timeit(hF, 3)
timeit(hG, 3)
timeit(hG)

```

Yields:

```julia
ans =

    0.1601

ans =

    0.1608

ans =

    0.0781

```

@Baicai_Xiao, Could you try my code above on your MATLAB?

Regarding MKL, Those are a “Must” videos for proper integration:

- [Speed Up Small Matrix Multiplication using New Intel® Math Kernel Library Capabilities](https://software.intel.com/en-us/videos/speed-up-small-matrix-multiplication-using-new-mkl-capabilities)
- [Sparse Linear Algebra Functions in Intel® Math Kernel Library](https://software.intel.com/en-us/videos/sparse-linear-algebra-functions-in-intel-math-kernel-library) (Sparse Solvers)

I wonder if this is the way Julia integrates MKL.

---

<div class="post-metadata">

**Author:** ![Baicai\_Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baicai_xiao/32/7395_2.png) [@Baicai\_Xiao](https://discourse.julialang.org/u/Baicai_Xiao)\
**Post date:** [March 13, 2019, 1:52pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/13 "2019-03-13T13:52:48Z")

</div>

I ran your code in my machine and got similar results  
`ans =

```
0.1109

```

ans =

```
0.1149

```

ans =

```
0.0555`

```

@c42f You are right, it is because I am not comparing the same thing. I am not a heavy matlab user, I didn’t realize the difference before. Thank you for pointing this out.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2024, 8:46am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/14 "2024-10-09T08:46:08Z")

</div>

Thank you for your post, I met the same situation. So the final solution is using a MKL package. I have tested it on my own window computer, and for matrices with dimensions (1000,1000), matlab and Julia’s svd matrix decomposition time is about the same. But when I tried to test the svd of an array with dimension (10000,10000), I found that matlab and Julia run about the same time for single-thread cases, but Julia is still significantly slower than matlab for multithreaded cases.  
6 threads:

```julia
#matlab
>> a=rand(10000,10000);
>> tic;[u,s,v]=svd(a);toc
Elapsed time is 141.392837 seconds.

```

```julia
#julia
using MKL
using LinearAlgebra
using BenchmarkTools

a=rand(10000,10000)
@btime u,s,v=svd(a)
>296.304 s (17 allocations: 4.47 GiB)

```

I don’t know what’s going on. Can someone help me with this?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 9, 2024, 9:33am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/15 "2024-10-09T09:33:02Z")

</div>

I checked on my PC. I can confirm the issue. I noticed that the CPU temperature when running the Julia code reached 70 °C, when running the Matlab code it reached 100 °C. In both cases the CPU load was 1600% (16 cores).

For me this looks as if Matlab is using different CPU commands than Julia+MKL.

I have a Ryzen 7950x CPU. Which CPU do you have? Perhaps MKL does not support AMD CPUs well?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 9, 2024, 9:59am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/16 "2024-10-09T09:59:25Z")

</div>

I’m almost certain that this is an insignificant blip next to `svd`’s runtimes, but generally interpolate global variables and constants `u,s,v = @btime svd($a)` in `BenchmarkTools` to leave out compiler shenanigans. I moved the assignments because they disappear inside the benchmark expression, which doesn’t seem to be your intent based on the MATLAB benchmark:

```julia
julia> @btime a = rand($1)
  23.015 ns (2 allocations: 64 bytes)
1-element Vector{Float64}:
 0.587895628234269

julia> a
ERROR: UndefVarError: `a` not defined in `Main`

```

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2024, 10:37am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/17 "2024-10-09T10:37:23Z")

</div>

> [@ufechner7](#):
>
> I have a Ryzen 7950x CPU. Which CPU do you have? Perhaps MKL does not support AMD CPUs well?

I have a Intel(R) Core™ i7-8700 CPU, not AMD CPUs. It seems that the problem is not caused by the CPU type.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2024, 10:49am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/18 "2024-10-09T10:49:39Z")

</div>

> [@Benny](#):
>
> I’m almost certain that this is an insignificant blip next to `svd`’s runtimes, but generally interpolate global variables and constants `u,s,v = @btime svd($a)` in `BenchmarkTools` to leave out compiler shenanigans. I moved the assignments because they disappear inside the benchmark expression, which doesn’t seem to be your intent based on the MATLAB benchmark:

Sorry, I cann’t get your idea, in another words, I don’t understand what this has to do with interpolation.

---

<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:** [October 9, 2024, 11:01am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/19 "2024-10-09T11:01:36Z")

</div>

`$` is the interpolation operator for strings and expressions. Here, `$a` interpolates the value of `a` into the benchmarking expression. You can read about it here: [Manual · BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Interpolating-values-into-benchmark-expressions)

The most common use of `$` is perhaps in string interpolation: [Strings · The Julia Language](https://docs.julialang.org/en/v1/manual/strings/#string-interpolation)

Example:

```julia
julia> str = "World"
"World"

julia> "Hello, $(str)!"
"Hello, World!"

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 9, 2024, 11:09am UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/20 "2024-10-09T11:09:09Z")

</div>

Also worth pointing out that `$`-interpolation is not a general feature in macro calls because source code doesn’t have `$`:

```julia
julia> macro pass(blah) blah end
@pass (macro with 1 method)

julia> @pass 1+1
2

julia> @pass $1+$1
ERROR: syntax: "$" expression outside quote around REPL[17]:1

```

Macros need to specially implement them for the context. `@eval` emulates `$`-interpolation in quotes that can be inputs to `eval`, while `BenchmarkTools.quasiquote!` collects `$`-interpolated expressions to be incorporated into the benchmark functions.

---

<div class="post-metadata">

**Author:** ![F-YF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f-yf/32/17363_2.png) [@F-YF](https://discourse.julialang.org/u/F-YF)\
**Post date:** [October 9, 2024, 12:19pm UTC](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645/21 "2024-10-09T12:19:58Z")

</div>

> [@DNF](#):
>
> `$` is the interpolation operator for strings and expressions. Here, `$a` interpolates the value of `a` into the benchmarking expression. You can read about it here: [Manual · BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Interpolating-values-into-benchmark-expressions)
> 
> The most common use of `$` is perhaps in string interpolation: [Strings · The Julia Language](https://docs.julialang.org/en/v1/manual/strings/#string-interpolation)
> 
> Example:

But that’s not why the time cost is slower than matlab, is it?

[Next page](https://discourse.julialang.org/t/svd-2x-slower-than-in-matlab-and-how-to-get-best-performance-on-windows10/21645.md?page=2)
