Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- numCol = 1500
- numRow = 3000
- mat = matrix(rnorm(numRow*numCol), numRow)
- a = ifelse(runif(numCol) < 0.1, NA, rnorm(numCol))
- b = ifelse(runif(numCol) < 0.1, NA, rnorm(numCol))
- for_ver = function(mat, a, b){
- loc_nonNA = which(!is.na(a)&!is.na(b))
- mat = mat[,loc_nonNA]
- a=a[loc_nonNA]
- b=b[loc_nonNA]
- for (i in 1:nrow(mat))
- mat[i, ]=(mat[i, ]-a)/b
- mat
- }
- sweep_ver = function(mat, a, b){
- loc_nonNA = which(!is.na(a)&!is.na(b))
- mat = mat[,loc_nonNA]
- a=a[loc_nonNA]
- b=b[loc_nonNA]
- mat=sweep(mat, 2, a, '-')
- mat=sweep(mat, 2, b, '/')
- mat
- }
- scale_ver = function(mat, a, b){
- loc_nonNA = which(!is.na(a)&!is.na(b))
- mat = mat[,loc_nonNA]
- a=a[loc_nonNA]
- b=b[loc_nonNA]
- mat=scale(mat, a, b)
- mat
- }
- sweep_ver2 = function(mat, a, b){
- mat=t(na.omit(t(rbind(mat, a, b))))
- a=mat[nrow(mat)-1, ]
- b=mat[nrow(mat), ]
- mat=mat[1:(nrow(mat)-2), ]
- mat=sweep(mat, 2, a, '-')
- mat=sweep(mat, 2, b, '/')
- mat
- }
- scale_ver2 = function(mat, a, b){
- mat=t(na.omit(t(rbind(mat, a, b))))
- a=mat[nrow(mat)-1, ]
- b=mat[nrow(mat), ]
- mat=mat[1:(nrow(mat)-2), ]
- mat=scale(mat, a, b)
- mat
- }
- apply_ver = function(mat, a, b){
- loc_nonNA = which(!is.na(a)&!is.na(b))
- t(apply(mat, 1, function(x) (x[loc_nonNA] - a[loc_nonNA]) / b[loc_nonNA]))
- }
- library(data.table)
- library(dplyr)
- DT = data.table(mat)
- data_table_ver = function(DT, a, b){
- loc_nonNA = which(!is.na(a)&!is.na(b))
- DT %>% select(loc_nonNA) %>%
- scale(a[loc_nonNA], b[loc_nonNA])
- }
- library(rbenchmark)
- benchmark(for_ver(mat, a, b), sweep_ver(mat, a, b), scale_ver(mat, a, b),
- sweep_ver2(mat, a, b), scale_ver2(mat, a, b), apply_ver(mat, a, b),
- data_table_ver(DT, a, b),
- columns = c("test", "replications", "elapsed", "relative"),
- order = "relative", replications = 20)
- # test replications elapsed relative
- # 1 for_ver(mat, a, b) 20 3.03 1.000
- # 6 apply_ver(mat, a, b) 20 3.80 1.254
- # 3 scale_ver(mat, a, b) 20 5.10 1.683
- # 2 sweep_ver(mat, a, b) 20 5.18 1.710
- # 7 data_table_ver(DT, a, b) 20 6.04 1.993
- # 4 sweep_ver2(mat, a, b) 20 9.75 3.218
- # 5 scale_ver2(mat, a, b) 20 10.07 3.323
- all.equal(for_ver(mat, a, b), scale_ver(mat, a, b), sweep_ver(mat, a, b),
- scale_ver2(mat, a, b), sweep_ver2(mat, a, b), , data_table_ver(DT, a, b),
- apply_ver(mat, a, b), check.attributes = FALSE) # TRUE
Advertisement
Add Comment
Please, Sign In to add comment