Hi Julia-folks!
I’m new to Julia (very new), but work mainly in R. I’m trying to replace a slow bit of R code with a Julia function that I will call from within R. Essentially what I’m trying to achieve is argmax
of a 4D array across the 4th dimension. My first attempt was based on argmax and mapslices to get the output format I need back in R:
A = rand(3, 10, 20, 5)
function whichmax4D1(array4d::Array{Float64, 4})
mapslices(argmax, array4d, dims=4)[:,:,:,]
end
@btime whichmax4D1(A)
1.084 ms (6059 allocations: 190.59 KiB)
So when called from within R this was slower than my R version. So I had a go at making things faster, based mainly on how I made the R version faster:
function whichmax4D2(array4d::Array{Float64, 4})
dims = size(array4d)
slicedim = dims[1:3]
num = prod(slicedim)
span = (1:dims[4]) *num .- num
res = Int[]
for i in 1:num
push!(res, findmax( array4d[i .+ span] )[2])
end
reshape(res, slicedim)
end
@btime whichmax4D2(A)
102.865 μs (612 allocations: 91.50 KiB)
res1 = whichmax4D1(A)
res2 = whichmax4D2(A)
res1 == res2
julia> res1 == res2
true
Yay so thats some progress - even with the overhead of calling from within R, its about twice as fast as my tuned R version. But I know almost zero Julia (these are my first Julia functions ), I figure there must be other ways to make this faster? Any suggestions? (I call this function alot small improvements here add up!)
Thank you all!