# Why Julia and linear algebra module are incredibly slow, compare to C++ and Eigen

**URL:** <https://discourse.julialang.org/t/why-julia-and-linear-algebra-module-are-incredibly-slow-compare-to-c-and-eigen/21304>\
**Category:** Performance\
**Tags:** question, package, performance, linearalgebra\
**Created:** [February 28, 2019, 2:32pm UTC](https://discourse.julialang.org/t/why-julia-and-linear-algebra-module-are-incredibly-slow-compare-to-c-and-eigen/21304 "2019-02-28T14:32:20Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 28, 2019, 7:59pm UTC](https://discourse.julialang.org/t/why-julia-and-linear-algebra-module-are-incredibly-slow-compare-to-c-and-eigen/21304/6 "2019-02-28T19:59:07Z")

</div>

Just wrapping your main code in a function and timing it with `@btime` from BenchmarkTools I got a runtime of 21us, which is several thousand times faster than what you’re seeing.

I also tried using StaticArrays, in which case runtime is entirely dominated by by the call to `nullspace` (btw. `nullspace` doesn’t work for static arrays, unfortunately): all the code, except `nullspace`, took 150ns to run, but then `nullspace(A)` takes 18us.

Here’s a simple benchmark without static arrays, but using BenchmarkTools:

```julia
function foo()
    K = [ 1169.19630 0.0 652.98743;
             0.0 1169.61014 528.83429;
             0.0 0.0 1.0]

    T = [0.961255 -0.275448 0.0108487 112.79;
         0.171961 0.629936 0.75737 -217.627;
        -0.21545 -0.72616 0.652895 1385.13]

    R = T[:, SVector(1,2,3)]
    t = T[:,end]

    tl = position_in_world(0.0, 0.0)
    tr = position_in_world(1123.0, 0.0)
    br = position_in_world(1123.0, 791.0)
    bl = position_in_world(0.0, 791.0)

    p1 = π_projection_function(K, R, t, tl)
    p2 = π_projection_function(K, R, t, tr)
    p3 = π_projection_function(K, R, t, br)
    p4 = π_projection_function(K, R, t, bl)
    P = [p1 p2 p3 p4]'

    M = [0.0 0.0 1.0;
       1123.0 0.0 1.0;
       1123.0 791.0 1.0;
             0.0 791.0 1.0]

    res = compute_homograpy_DLT(M, P)
    homography = reshape(res, (3,3))'
    return homography
end

```

Then benchmark it:

```julia
julia> @btime bar()
  20.643 μs (65 allocations: 14.48 KiB)
3×3 Adjoint{Float64,Array{Float64,2}}:
  0.000244416 -0.000198718 0.915494  
  2.16745e-5 8.80403e-5 0.40233   
 -5.35586e-8 -1.81231e-7 0.00140359

```

**Edit:** To clarify, the problem isn’t with your code, but with your benchmarking.

---

_[View the full topic](https://discourse.julialang.org/t/why-julia-and-linear-algebra-module-are-incredibly-slow-compare-to-c-and-eigen/21304)._
