3 回答

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