You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi!
I came up with a faster version of permute(). Maybe it lacks some functionality or generality, but I couldn't come up with such a case yet - can you?
using AxisArrays
using BenchmarkTools
# the wooden hammer@inline@inboundsfunctioncheck_duplicates(arr)
N =length(arr)
for i in1:N-1for j in i+1:N
arr[i] != arr[j] ||throw(ArgumentError("duplicate"))
endendreturnnothingendfunctionfoo_perm(to, from)
length(to) ==length(from) ||throw(ArgumentError("not same length"))
res =Vector{Int}(undef, length(from))
@inboundsfor (i,t) inenumerate(to)
idx =findfirst(from .== t)
idx !=nothing||throw(ArgumentError("a not in b"))
res[i] = idx
endcheck_duplicates(res)
return res
end
to=(:c,:w,:h,:d)
from=(:c,:h,:w,:d)
foo_perm(to, from) == AxisArrays.permutation(to, from)
@btimefoo_perm($to, $from) # 42ns@btime AxisArrays.permutation($to, $from) # 307ns
I think axisnames() is typically called with an AxisArray as argument, but I found it a bit slow. Again I came up with another solution which might lack generality, but is faster:
axname(a::AxisArrays.Axis{name}) where name = name
axnames(a::AxisArray) =axname.(a.axes)
a =AxisArray(rand(3,4,5), :c,:h,:w)
axnames(a) ==axisnames(a)
@btimeaxnames($a) # 340ns@btimeaxisnames($a) # 4us
The text was updated successfully, but these errors were encountered:
maxfreu
changed the title
Faster version of permute and axisnames
Faster version of permutation() and axisnames()
Jun 25, 2020
Hi!
I came up with a faster version of permute(). Maybe it lacks some functionality or generality, but I couldn't come up with such a case yet - can you?
I think axisnames() is typically called with an AxisArray as argument, but I found it a bit slow. Again I came up with another solution which might lack generality, but is faster:
The text was updated successfully, but these errors were encountered: