3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
R_execClosure
eval.c
apply
mean
apply
A = matrix(as.numeric(1:100000))
system.time({ Sum = 0 for (i in seq_along(A)) { Sum = Sum + A[[i]] } Sum})
sum(A)
sum
# 0.370 secondssystem.time({ I = 0 while (I < 100000) { 10 I = I + 1 }})# 0.743 seconds -- double the time just adding parenthesessystem.time({ I = 0 while (I < 100000) { ((((((((((10)))))))))) I = I + 1 }})
(
> `(` = function(x) 2> (3)[1] 2
(

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
apply
尤伊·利格斯和約翰·??怂埂7?wù)臺(tái):我怎樣才能避免這個(gè)循環(huán),或者讓它更快?r新聞,8(1):46-50,2008年5月。
許多關(guān)于R狀態(tài)的評(píng)論認(rèn)為使用循環(huán)是一個(gè)特別糟糕的主意。這不一定是真的。在某些情況下,很難編寫(xiě)向量化代碼,或者向量化代碼可能會(huì)消耗大量?jī)?nèi)存。
在循環(huán)之前將新對(duì)象初始化為完整長(zhǎng)度,而不是在循環(huán)中增加它們的大小。 不要在循環(huán)中做可以在循環(huán)之外完成的事情。 不要為了避免循環(huán)而避免循環(huán)。
for
apply
- 3 回答
- 0 關(guān)注
- 585 瀏覽
添加回答
舉報(bào)