@ tushaR правильно понял для базы R.:)
mat <- sample.matrix <- matrix(1:33635,ncol=35)
dist_fun <- function(x, y) sqrt(sum(x-y)**2)
s_fun <- function(t){
sapply(t, function(x) dist_fun(mat[x,], mat[x+1]) )
}
m_fun <- function(t){
mapply(function(x) dist_fun(mat[x,], mat[x+1]), t)
}
a_fun <- function(t){
apply(matrix(t, nrow = 1), 2, function(x) dist_fun(mat[x,], mat[x+1]) )
}
l_fun <- function(t){
unlist( lapply(as.list(t, nrow = 1), function(x) dist_fun(mat[x,], mat[x+1]) ) )
}
# t <- 1:(nrow(mat)-1)
# s_fun( 1:(nrow(mat)-1) )
library(microbenchmark)
n <- 1e5 # 961
mat <- sample.matrix <- matrix(1:(35*n),ncol=35)
microbenchmark("sapply" = s_fun(1:(nrow(mat)-1)),
"mapply" = m_fun(1:(nrow(mat)-1)),
"apply" = a_fun(1:(nrow(mat)-1)),
"lapply" = l_fun(1:(nrow(mat)-1)),
list = NULL, times = 100L, unit = "ms", check = NULL,
control = list(), setup = NULL)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> sapply 315.5892 413.6667 534.0462 498.0186 600.1315 1096.092 100
#> mapply 313.7013 441.9728 534.9250 505.3026 577.5770 1167.973 100
#> apply 387.1655 503.9833 615.6288 563.4751 665.4584 1571.387 100
#> lapply 309.3762 416.0796 553.0482 491.5356 645.2026 1823.269 100
Создано в 2019-06-04 пакетом Представ (v0.2.1)