# Copy and collect slow on QRCompactWYQ

**URL:** <https://discourse.julialang.org/t/copy-and-collect-slow-on-qrcompactwyq/23145>\
**Category:** Performance\
**Tags:** linearalgebra\
**Created:** [April 14, 2019, 7:39pm UTC](https://discourse.julialang.org/t/copy-and-collect-slow-on-qrcompactwyq/23145 "2019-04-14T19:39:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [April 14, 2019, 7:39pm UTC](https://discourse.julialang.org/t/copy-and-collect-slow-on-qrcompactwyq/23145/1 "2019-04-14T19:39:08Z")

</div>

The functions `copy` and `collect` are extremely slow on the results of a QR factorization. `Matrix` produces the same result, but is much faster. An easy solution is to not use `copy` or `collect`. Is there a good reason for these routines to be so much slower? If not, fixing this is sure to help some people.

The following example is with Julia 1.1.0, on an iMac.

```julia
julia> n = 10
julia> q = qr(randn(n,n)).Q;
julia> @btime x = copy($q);
  81.203 μs (301 allocations: 47.75 KiB)

julia> @btime y = collect($q);
  80.980 μs (301 allocations: 47.75 KiB)

julia> @btime z = Matrix($q);
  5.253 μs (2 allocations: 1.75 KiB)

julia> x == y == z
true

julia> n = 100;
julia> q = qr(randn(n,n)).Q;
julia> @btime x = copy($q);
  114.106 ms (30002 allocations: 25.71 MiB)

julia> @btime y = collect($q);
  113.197 ms (30002 allocations: 25.71 MiB)

julia> @btime z = Matrix($q);
  114.343 μs (4 allocations: 156.41 KiB)

```

As you can imagine, it becomes much worse with larger matrices.

---

<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:** [April 14, 2019, 9:47pm UTC](https://discourse.julialang.org/t/copy-and-collect-slow-on-qrcompactwyq/23145/2 "2019-04-14T21:47:31Z")

</div>

`@which` is a good way to figure out what the different functions are calling. I suspect `Matrix` is overloaded specifically for `QRCompactWY` to call the fast `lmul!` implementation while the other routines still use the generic fallbacks based on `getindex`.
