[問題] 在function中引function比較慢, why?
效能諮詢(我想讓R 跑更快)
[問題敘述]:
在function中引function, 主要是為了好讀好修. 可是這樣就很慢.
請問有什麼方式加速嗎? 例如引用的方法?
[程式範例]:
fun0 = function(fun){
x=1:10
y=10:1
fun1 = function(x,y){z=xy}
fun2 = function(x,y){z=x*y}
if(fun == "plus") z = fun1(x,y) else{
if(fun == "product") z = fun2(x,y)
}
return(z)
}
system.time(for(i in 1:1e7) fun0("product"))
user system elapsed
12.70 0.00 12.72
# -----------------------------------------
for(i in 1:10) gc()
fun0 = function(fun){
x=1:10
y=10:1
if(fun == "plus") z=xy else{
if(fun == "product") z=x*y
}
return(z)
}
system.time(for(i in 1:1e7) fun0("product"))
user system elapsed
7.69 0.00 7.73
[環境敘述]:
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
(Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.4 tools_3.4.4 yaml_2.1.18
[關鍵字]:
選擇性,也許未來有用
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 213.47.170.138
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1522794834.A.96D.html
推
04/04 11:22,
7年前
, 1F
04/04 11:22, 1F
→
04/04 12:11,
7年前
, 2F
04/04 12:11, 2F
→
04/04 16:41,
7年前
, 3F
04/04 16:41, 3F
→
04/04 16:42,
7年前
, 4F
04/04 16:42, 4F
→
04/04 16:42,
7年前
, 5F
04/04 16:42, 5F
→
04/04 16:42,
7年前
, 6F
04/04 16:42, 6F
→
04/04 18:38,
7年前
, 7F
04/04 18:38, 7F
→
04/04 18:40,
7年前
, 8F
04/04 18:40, 8F
→
04/04 18:41,
7年前
, 9F
04/04 18:41, 9F
→
04/04 19:09,
7年前
, 10F
04/04 19:09, 10F
→
04/04 19:10,
7年前
, 11F
04/04 19:10, 11F
→
04/04 21:57,
7年前
, 12F
04/04 21:57, 12F
→
04/04 22:00,
7年前
, 13F
04/04 22:00, 13F
→
04/04 22:00,
7年前
, 14F
04/04 22:00, 14F
→
04/04 22:01,
7年前
, 15F
04/04 22:01, 15F
→
04/04 22:02,
7年前
, 16F
04/04 22:02, 16F
→
04/04 22:02,
7年前
, 17F
04/04 22:02, 17F
→
04/04 22:32,
7年前
, 18F
04/04 22:32, 18F
→
04/04 22:33,
7年前
, 19F
04/04 22:33, 19F
→
04/04 22:33,
7年前
, 20F
04/04 22:33, 20F
→
04/04 22:35,
7年前
, 21F
04/04 22:35, 21F
→
04/04 22:35,
7年前
, 22F
04/04 22:35, 22F
→
04/04 22:36,
7年前
, 23F
04/04 22:36, 23F
→
04/04 22:36,
7年前
, 24F
04/04 22:36, 24F
→
04/04 22:38,
7年前
, 25F
04/04 22:38, 25F
→
04/04 22:39,
7年前
, 26F
04/04 22:39, 26F
→
04/04 23:04,
7年前
, 27F
04/04 23:04, 27F
→
04/04 23:48,
7年前
, 28F
04/04 23:48, 28F
→
04/04 23:48,
7年前
, 29F
04/04 23:48, 29F
→
04/04 23:48,
7年前
, 30F
04/04 23:48, 30F
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章