Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove cat(X...) statements #274

Closed
odunbar opened this issue Apr 7, 2023 · 1 comment · Fixed by #283
Closed

remove cat(X...) statements #274

odunbar opened this issue Apr 7, 2023 · 1 comment · Fixed by #283

Comments

@odunbar
Copy link
Collaborator

odunbar commented Apr 7, 2023

For convenience we often use cat(X..., dims=p) statements to concatenate vectors and matrices. During some profile I see these become terribly inefficient at even moderate problems. We should probably remove them. As an example"

X = [zeros(3,3) for i = 1:1000]

# option 1 
xx = cat(X...,dims=3)

# option 2
function squidge(X::AbstractVector) 
       xx = zeros(size(X[1],1),size(X[1],2),length(X))
       for i = 1:length(X)
           xx[:,:,i] = X[i]
       end
       return xx
end
xx = squidge(X)

option 1 gives e.g.:
0.037136 seconds (10.01 k allocations: 13.965 MiB)
option 2 gives e.g.:
0.000088 seconds (2 allocations: 70.375 KiB)

Action

Replace cat(X...,dims=p) with the appropriate loops.

@haakon-e
Copy link
Member

Alternative with julia base functions (ref):

function squidge2(x)
    B = reduce(hcat, x);
    sz = size(X[1])
    C = reshape(B, sz[1], sz[2], :);
end

Gets: 0.000073 seconds (6 allocations: 70.516 KiB)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants