看板 R_Language 關於我們 聯絡資訊
#BAD example: without memory preallocation intersection <- names(a.list)[names(a.list) %in% names(b.list)] result <- as.list(NULL) for(i in intersection){ result[[i]] <- sum(table(a.list[[i]][a.list[[i]] %in% b.list[[i]]])) } #Fixed: intersection <- names(a.list)[names(a.list) %in% names(b.list)] result <- vector("list", length(intersection)) names(result) <- intersection for(i in intersection){ result[[i]] <- sum(table(a.list[[i]][a.list[[i]] %in% b.list[[i]]])) #cat(tracemem(result), '\n') } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.135.208 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1446790865.A.50F.html ※ 編輯: CKPILOT (123.193.135.208), 11/06/2015 14:22:23
celestialgod: 建議:append一個NULL的list並非好習慣... 11/06 14:25
celestialgod: 還是先preallocate: result=vector('list', length( 11/06 14:25
celestialgod: intersection)) 11/06 14:25
celestialgod: #1LjDrSmd (R_Language) 11/06 14:27
CKPILOT: Thanks, it's really important for mem allocation 11/06 14:31
※ 編輯: CKPILOT (123.193.135.208), 11/06/2015 15:19:20 ※ 編輯: CKPILOT (123.193.135.208), 11/06/2015 15:20:28