# QR Decomposition Extremely Slow

**URL:** https://discourse.julialang.org/t/qr-decomposition-extremely-slow/16946
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [October 30, 2018, 1:24am UTC](https://discourse.julialang.org/t/qr-decomposition-extremely-slow/16946 "2018-10-30T01:24:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [October 30, 2018, 1:24am UTC](https://discourse.julialang.org/t/qr-decomposition-extremely-slow/16946/1 "2018-10-30T01:24:25Z")

</div>

It seems converting a Q matrix to a Float Matrix takes high-degree polynomial time at best.

Is this a bug? If so, is there any way to work around it? I don’t remember having this problem on earlier versions (I am using 1.01). Granted my laptop is pretty slow, but this is worse than matrix inversion, so the performance doesn’t make a lot of sense to me.

```julia
using LinearAlgebra
function mwe()
  local Q::Matrix{Float64}

  X = rand(200,13)
  f = qr(X)
  @time Q = Matrix{Float64}(f.Q)

  X = rand(400,13)
  f = qr(X)
  @time Q = Matrix{Float64}(f.Q)

  X = rand(600,13)
  f = qr(X)
  @time Q = Matrix{Float64}(f.Q)

  X = rand(800,13)
  f = qr(X)
  @time Q = Matrix{Float64}(f.Q)
end

mwe()

```

Output:  
0.260873 seconds (120.00 k allocations: 145.569 MiB, 1.49% gc time)  
2.115116 seconds (480.00 k allocations: 1.022 GiB, 3.70% gc time)  
6.812497 seconds (1.08 M allocations: 3.372 GiB, 4.46% gc time)  
15.563167 seconds (1.92 M allocations: 7.901 GiB, 3.08% gc time)

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [October 30, 2018, 2:15am UTC](https://discourse.julialang.org/t/qr-decomposition-extremely-slow/16946/2 "2018-10-30T02:15:52Z")

</div>

I am seeing similar behavior in 1.0 on a Windows 10 machine. I would say this is a bug. Unless someone else sees an obvious problem with your code, I suggest that you open an issue.

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [October 30, 2018, 2:26am UTC](https://discourse.julialang.org/t/qr-decomposition-extremely-slow/16946/3 "2018-10-30T02:26:10Z")

</div>

> [@QR decomposition in julia 2x slower than octave](https://discourse.julialang.org/t/qr-decomposition-in-julia-2x-slower-than-octave/10043/3):
>
> I suspect they may be doing different things. I don’t know what [qr method](https://octave.sourceforge.io/octave/function/qr.html) Octave calls when no output arguments are provided, but my guess is that it’s just doing the 1st phase (computing the Householder reflectors and upper-triangular part) and not the 2nd (building Q). This would roughly match [qrfact](https://docs.julialang.org/en/stable/stdlib/linalg/#Base.LinAlg.qrfact) in Julia. On my machine, I get: julia\> @time qrfact(A); 5.906331 seconds (25.99 k allocations: 194.912 MiB, 2.13% gc time) julia\> @time qr(A); 16.202900 seconds (93.73 k allocations: 770.718 …

---

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [October 30, 2018, 2:36am UTC](https://discourse.julialang.org/t/qr-decomposition-extremely-slow/16946/4 "2018-10-30T02:36:08Z")

</div>

Issue opened: [# 29846](https://github.com/JuliaLang/julia/issues/29846#issue-375293067)

---

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [October 30, 2018, 3:18am UTC](https://discourse.julialang.org/t/qr-decomposition-extremely-slow/16946/5 "2018-10-30T03:18:17Z")

</div>

RalphAS on the GitHub issue thread ([# 29846](https://github.com/JuliaLang/julia/issues/29846#issue-375293067)) noted that we can drop the type parameter and recover performance, e.g. writing `Matrix{Float64}(f.Q)`. This workaround fixed the slowness in my code, so I will count the question as solved.
