# Pseudo-inverse of large matrix very slow

**URL:** https://discourse.julialang.org/t/pseudo-inverse-of-large-matrix-very-slow/49114
**Category:** New to Julia
**Tags:** question, performance, linearalgebra, python
**Created:** [October 27, 2020, 11:47am UTC](https://discourse.julialang.org/t/pseudo-inverse-of-large-matrix-very-slow/49114 "2020-10-27T11:47:15Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 27, 2020, 11:55am UTC](https://discourse.julialang.org/t/pseudo-inverse-of-large-matrix-very-slow/49114/2 "2020-10-27T11:55:03Z")

</div>

> [@danvip](#):
>
> I seem to get slower computation times than with python/numpy.

Probably it’s using a different number of threads?

You can call `LinearAlgebra.BLAS.set_num_threads` for OpenBLAS in Julia. I think with MKL you have to set the `MKL_NUM_THREADS` environment variable (before launching Julia)?

PS. If you are solving a least-square or similar non-invertible system, in most cases you should use pivoted QR rather than a pseudo-inverse, via `A \ b` … or equivalently by `QR = qr(A, Val{true}())` followed by `QR \ b` (if you want to re-use the factorization for multiple right-hand sides). See also [this post](https://discourse.julialang.org/t/efficient-way-of-doing-linear-regression/31232/33). (If you have an _underdetermined_ solve and want the minimum-norm solution, you can [use the LQ factorization](https://github.com/JuliaLang/julia/pull/34350) instead.)

> [@danvip](#):
>
> In Python, the pseudo-inverse is a clear bottleneck in terms of computation time, so I decided to look at Julia as an alternative, to check if it could improve the times.

Switching languages will not help you here. Everyone is using the same dense linear-algebra libraries (LAPACK with some backend, typically either OpenBLAS or MKL). The only way to improve your performance if you are limited by LAPACK is by changing your algorithm (e.g. to use QR rather than SVD, or doing something even more clever).

Julia helps when you need to write your _own_ performance-critical inner-loop code.

---

_[View the full topic](https://discourse.julialang.org/t/pseudo-inverse-of-large-matrix-very-slow/49114)._
