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

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:

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> @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.

8 Likes