# svdvals is alarmingly slow

**URL:** <https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259>\
**Category:** Performance\
**Created:** [February 22, 2018, 6:29pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259 "2018-02-22T18:29:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [February 22, 2018, 6:29pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/1 "2018-02-22T18:29:33Z")

</div>

Straightforward use of `svdvals` on a freshly restarted Julia session:

```julia
using BenchmarkTools
B=randn(100,100);
@btime svdvals($B);
versioninfo()

```

Here’s the result.

```julia
  10.423 ms (11 allocations: 138.20 KiB)

Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

```

Run seconds later in MATLAB:

```julia
>> B=randn(100);
>> tic, for k=1:10000, svd(B); end, t=toc/10000
t =
     5.541419457000000e-04

```

(The results don’t change much for different random matrices.) So Julia is taking nearly 20x as long to compute the singular values. Have I overlooked something?

– Toby

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [February 22, 2018, 6:43pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/2 "2018-02-22T18:43:40Z")

</div>

On my computer:

```julia
julia> @btime svdvals($B);
  710.531 μs (11 allocations: 138.20 KiB)

julia> versioninfo()
Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Prescott)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

```

while in Matlab I get:

```julia
t =

   6.2645e-04

```

(but Julia is running in VirtualBox and Matlab natively in Windows).

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [February 22, 2018, 7:00pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/3 "2018-02-22T19:00:01Z")

</div>

Interesting, I’m getting slow times on my Mac as well, but not when run on the same machine in a Linux VM.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [February 22, 2018, 7:01pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/4 "2018-02-22T19:01:17Z")

</div>

It was about 0.8 ms for me on Linux. Just for the hell of it I tried

```julia
sqrt.(eigvals(B'*B))

```

and got `760.292 μs (45 allocations: 196.22 KiB) `, are these exactly equivalent in `Base`?

It’s gratifying that `sqrt.(eigvals(B'*B))` is so close to MATLAB’s `svdvals` when things are working properly 😄.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [February 22, 2018, 7:04pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/5 "2018-02-22T19:04:59Z")

</div>

I’ve opened an issue here: [https://github.com/JuliaLang/julia/issues/26162](https://github.com/JuliaLang/julia/issues/26162)

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [February 22, 2018, 7:14pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/6 "2018-02-22T19:14:29Z")

</div>

Unfortunately we’re comparing apples to oragnes here: Julia’s `svdvals` vs Matlab’s `svd`. (Matlab doesn’t have an equivalent to `svdvals` AFAIK). Julia’s `svdfact` is 6x slower than Matlab’s `svd` on my machine.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [February 22, 2018, 7:17pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/7 "2018-02-22T19:17:49Z")

</div>

Oh well, I had hope.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [February 22, 2018, 7:29pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/8 "2018-02-22T19:29:09Z")

</div>

> [@mbaz](#):
>
> (Matlab doesn’t have an equivalent to svdvals AFAIK)

I think the single output argument of Matlab `svd` is equivalent to Julia’s `svdvals` (I don’t know enough Matlab to say which is called with no arguments).

---

<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:** [February 22, 2018, 7:41pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/9 "2018-02-22T19:41:05Z")

</div>

We have seen that [OpenBLAS is slower than Intel MKL](https://discourse.julialang.org/t/benchmark-matlab-julia-for-matrix-operations).

I wonder if it has to do with that.  
Does `svdvals` use OpenBLAS?

I suggest you try the JuliaPro distribution with Intel MKL.

---

<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:** [February 22, 2018, 8:04pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/10 "2018-02-22T20:04:04Z")

</div>

I always build Julia with Mac’s system BLAS because OpenBLAS is so slow

---

<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:** [February 22, 2018, 8:21pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/11 "2018-02-22T20:21:01Z")

</div>

@barche’s benchmarks show that OpenBLAS is a big part of it:

[https://github.com/barche/julia-blas-benchmarks/blob/master/BenchmarkResults.ipynb](https://github.com/barche/julia-blas-benchmarks/blob/master/BenchmarkResults.ipynb)

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [February 22, 2018, 8:38pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/12 "2018-02-22T20:38:47Z")

</div>

> [@simonbyrne](#):
>
> I think the single output argument of Matlab svd is equivalent to Julia’s svdvals (I don’t know enough Matlab to say which is called with no arguments).

That is correct. So, Matlab 2017b on windows:

```julia
>> tic, for k=1:10000, s=svd(B); end, t=toc/10000
t =
    5.8891e-04

which is slightly faster than Julia running on Linux in a virtual machine.
```

---

<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:** [February 22, 2018, 8:55pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/13 "2018-02-22T20:55:23Z")

</div>

Try it with Julia MKL to do a more 1-1 comparison.

---

<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:** [February 22, 2018, 8:58pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/14 "2018-02-22T20:58:23Z")

</div>

> [@mbaz](#):
>
> > [@simonbyrne](#):
> >
> > I think the single output argument of Matlab svd is equivalent to Julia’s svdvals (I don’t know enough Matlab to say which is called with no arguments).
> 
> That is correct. So, Matlab 2017b on windows:
> 
> ```julia
> >> tic, for k=1:10000, s=svd(B); end, t=toc/10000
> t =
> 5.8891e-04
> 
> which is slightly faster than Julia running on Linux in a virtual machine.
> 
> ```

That doesn’t show the equivalence between MATLAB’s `svd()` with one output to Julia’s `svdvals()`.

You need to show that in case you call MATLAB `svd()` with 2 output parameter the run time changes.  
This will imply that in case of one output MATLAB is doing something optimized for getting the Singular Values only.

---

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [February 22, 2018, 9:40pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/15 "2018-02-22T21:40:18Z")

</div>

Took the advice of @RoyiAvital and @ChrisRackauckas to download JuliaPro with MKL. Timing went down to about 500 μs, which is completely on par with my MATLAB results.

Funny, I always just assumed the “Pro” meant $$. So I went with more DYI, except not enough DYI to build from source…

Thanks to all for the quick answers.

---

<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:** [February 22, 2018, 9:42pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/16 "2018-02-22T21:42:43Z")

</div>

> [@tobydriscoll](#):
>
> So I went with more DYI, except not enough DYI to build from source…

Yeah, I think there’s some licensing reason why it’s not default? 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [February 22, 2018, 10:22pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/17 "2018-02-22T22:22:00Z")

</div>

> [@RoyiAvital](#):
>
> You need to show that in case you call MATLAB svd() with 2 output parameter the run time changes.

OK, just to summarize my timings:

Matlab, `[u,s,v] = svd(B)` : 1.4 ms  
Julia, `s=svdfact($B)` : 2.38 ms

Matlab, `s = svd(B)` : 585 us  
Julia, `s = svdvals(B)` : 673 us

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [February 23, 2018, 12:08am UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/18 "2018-02-23T00:08:32Z")

</div>

Perhaps we should just use the system BLAS on OS X (since it is the one platform where the system BLAS is actually good).

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [February 23, 2018, 8:16pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/19 "2018-02-23T20:16:05Z")

</div>

@mbaz: are you able to try it on the Julia Windows binary? I would be interested to know if it is a problem there as well.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [February 23, 2018, 8:52pm UTC](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259/20 "2018-02-23T20:52:56Z")

</div>

I don’t have Julia installed in Windows, but I’ll try to find time this weekend to do it.

[Next page](https://discourse.julialang.org/t/svdvals-is-alarmingly-slow/9259.md?page=2)
