-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Open
Labels
performanceMust go fasterMust go faster
Description
permute!(v, p)
is strictly worse than v[p]
. It is slower, allocates more memory, and doesn't work for multidimensional arrays. It shouldn't exist. I think the following alternatives are reasonable:
- Deprecate it.
- Write instead something like
function permute!(v::AbstractArray, p::AbstractArray)
for i in 1:length(p)-1
ind = p[i]
while (ind < i)
ind = p[ind]
end
if i != ind
temp = v[i]
v[i] = v[ind]
v[ind] = temp
end
end
return v
end
This is much slower but at least it doesn't allocate. EDIT: Copied correct in-place algorithm from StackOverflow.
3. Define permute!(dest, src, p) = @views dest .= src[p]
, which is faster and doesn't allocate.
I'm happy to do a PR for any of the alternatives if there's agreement. Not also that invpermute!
suffers from similar problems.
mbauman
Metadata
Metadata
Assignees
Labels
performanceMust go fasterMust go faster