# Why is work with Q in qr() is so expensive?

**URL:** https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143
**Category:** Performance
**Tags:** bug, linearalgebra, qr
**Created:** [April 13, 2021, 3:51am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143 "2021-04-13T03:51:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)
#### Post date: [April 13, 2021, 3:51am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143/1 "2021-04-13T03:51:51Z")

</div>

In the picture below, we can see that it is very efficient to generate a random unitary matrix `Q `using the `qr()` function in LinearAlgebra.

However, if we want to work with the generated `Q`, it appears to be very expensive in memory and time. With size 401 by 401, `Q*1` takes 24 seconds and 1.5 GiB of memory, with size 500 by 500 it appears to take forever.

I wonder what is the reason for this, and how to walk around this issue.

🙏Thank you!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/4/b4d33350a27404016594cd14018c95c57cd0866b.png)

---

<div class="post-metadata">

### Author: ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)
#### Post date: [April 13, 2021, 4:38am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143/2 "2021-04-13T04:38:44Z")

</div>

this is really strange and could be a bug.  
Try `Q = convert(Matrix,qr(A).Q) * 1` as a workaround

---

<div class="post-metadata">

### Author: ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)
#### Post date: [April 13, 2021, 4:41am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143/3 "2021-04-13T04:41:49Z")

</div>

Works ! 🙏

---

<div class="post-metadata">

### Author: ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)
#### Post date: [April 13, 2021, 7:14am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143/4 "2021-04-13T07:14:24Z")

</div>

This appears to hit a very slow fallback:

```julia
julia> @which Q*1
*(A::AbstractArray, B::Number) in Base at arraymath.jl:55

```

Can you open an issue?

---

<div class="post-metadata">

### Author: ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)
#### Post date: [April 13, 2021, 7:47am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143/5 "2021-04-13T07:47:32Z")

</div>

I have just raised an issue [here](https://github.com/JuliaLang/julia/issues/40455).

---

<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: [April 13, 2021, 8:44am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143/6 "2021-04-13T08:44:34Z")

</div>

I have also responded in the issue, but let me repeat here for convenience:

```julia
julia> for i in 1:100:500

           A = randn(i,i)
           println("Size = $i")
           printstyled("Time took for q : ", color=:green)
           @time Q = qr(A).Q
           printstyled("Time took for Q*1 : ", color=:red)
           @time Q = Q*1
           println("")
       end
Size = 1
Time took for q : 0.000008 seconds (5 allocations: 352 bytes)
Time took for Q*1 : 0.000010 seconds (2 allocations: 192 bytes)

Size = 101
Time took for q : 0.000418 seconds (8 allocations: 136.922 KiB)
Time took for Q*1 : 0.000158 seconds (4 allocations: 159.656 KiB)

Size = 201
Time took for q : 0.000888 seconds (8 allocations: 429.109 KiB)
Time took for Q*1 : 0.000433 seconds (4 allocations: 631.531 KiB)

Size = 301
Time took for q : 0.001847 seconds (8 allocations: 877.547 KiB)
Time took for Q*1 : 0.001077 seconds (4 allocations: 1.383 MiB)

Size = 401
Time took for q : 0.003631 seconds (8 allocations: 1.447 MiB)
Time took for Q*1 : 0.003361 seconds (4 allocations: 2.454 MiB)

julia> @which qr(randn(2,2)).Q * 1
*(A::LinearAlgebra.AbstractQ, b::Number) in LinearAlgebra at /Users/crstnbr/repos/julia/usr/share/julia/stdlib/v1.7/LinearAlgebra/src/qr.jl:618

julia> versioninfo()
Julia Version 1.7.0-DEV.915
Commit 2ba139c48e* (2021-04-13 04:20 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin20.3.0)
  CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = subl
  JULIA_NUM_THREADS = 6
  JULIA_PKG_SERVER = eu-central.pkg.julialang.org

```

So this issue seems to be fixed on master (Julia 1.7-DEV)

---

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [April 13, 2021, 9:33am UTC](https://discourse.julialang.org/t/why-is-work-with-q-in-qr-is-so-expensive/59143/7 "2021-04-13T09:33:42Z")

</div>

For reference: It was fixed in the [PR #39533](https://github.com/JuliaLang/julia/pull/39533).
