寻找多维数组代码的加速 [英] Looking for speedups for multidimensional array code
问题描述
我有 2 个多维数组 - 一个 4D 数组和一个 3D 数组 - 以及一些代码来查找沿维度的 4D 数组的最大值,并基于此从 3D 数组中进行选择.目前它很慢,我想加快速度.
I have 2 multidimensional arrays - a 4D array and a 3D array - and some code to to find the maximum of the 4D array along a dimension, and make an index for selecting from the 3D array based on this. At the moment it's quite slow and I'd like to speed things up.
正则表达式:
library(microbenchmark)
# Make some arrays to test with
array4d <- array( runif(5*500*50*5 ,-1,0),
dim = c(5, 500, 50, 5) )
array3d <- array( runif(5*500*5, 0, 1),
dim = c(5, 500, 5))
# The code of interest
microbenchmark( {
max_idx <- apply(array4d, c(1,2,3), which.max )
selections <- list()
for( i in 1:dim(array4d)[3] ){
selections[[i]] <- apply(array3d, c(1,2), which.max) == max_idx[ , , i]
}
})
感谢任何提示!
(一个附带问题是我正在考虑将 which.max
替换为 nnet::which.is.max
以随机打破关系)
(A side issue is I'm considering replacing which.max
by nnet::which.is.max
to have random breaking of ties)
感谢@GKi,一个更快的解决方案,但我仍然希望有一些加速:
A faster solution thanks to @GKi, but I'm still hoping for some speedups:
max_idx <- apply(array4d, c(1,2,3), which.max)
max_idx2 <- apply(array3d, c(1,2), which.max)
selections <- lapply(seq_len(dim(array4d)[3]), function(i) max_idx2 == max_idx[ , , i])
推荐答案
你可以把 apply(array3d, c(1,2), which.max)
放在循环之外.
You can put apply(array3d, c(1,2), which.max)
outside the loop.
microbenchmark( {
max_idx <- apply(array4d, c(1,2,3), which.max)
max_idx2 <- apply(array3d, c(1,2), which.max)
selections <- lapply(seq_len(dim(array4d)[3]), function(i) max_idx2 == max_idx[ , , i])
},
{
max_idx <- apply(array4d, c(1,2,3), which.max )
selections <- list()
for( i in 1:dim(array4d)[3] ){
selections[[i]] <- apply(array3d, c(1,2), which.max) == max_idx[ , , i]
}
})
# min lq mean median uq max neval cld
# 204.1650 228.0010 260.3101 256.0132 271.6664 433.8932 100 a
# 396.5284 448.4167 495.3885 487.7741 530.9028 693.5601 100 b
这篇关于寻找多维数组代码的加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!