Y.A.t.Q : Yet Another @threads Question

Beautiful fast code:

function sequenceCosine(X_sub::Matrix, X::Matrix)
    results = similar(X, size(X_sub, 1), size(X, 1))
    for i in axes(X_sub, 1)
        for j in axes(X, 1)
            results[i, j] = @views cosine_dist(X_sub[i, :], X[j, :])
        end
    end
    results
end

function parallelCosine(X_sub::Matrix, X::Matrix)
    results = similar(X, size(X_sub, 1), size(X, 1))
    @threads for j in axes(X, 1)
        for i in axes(X_sub, 1)
            results[i, j] = @views cosine_dist(X_sub[i, :], X[j, :])
        end
    end
    results
end

with result

  4.377 s (2 allocations: 61.04 MiB)
  604.920 ms (33 allocations: 61.04 MiB)
3 Likes