[R] memory in R

Spencer Graves spencer.graves at pdf.com
Fri Jan 16 15:05:45 CET 2004


To expand a bit on Uwe's comments:  R is vectorized and loops are 
notoriously inefficient.  Consider the following: 

start.time <- proc.time()
set.seed(1)
X <- array(rnorm(1000), dim=c(100, 10))
X.1 <- apply(X, 2, mean)
print(var(X.1))
(elapsed.time <- proc.time()-start.time)

start.ttime <- proc.time()
set.seed(1)
X <- array(NA, dim=c(100, 10))
for(j in 1:10)for(i in 1:100)X[i,j] <- rnorm(1)
X. <- rep(0, 10)
X.. <- 0
varX <- 0
for(j in 1:10){
    for(i in 1:100)
        (X.[j] <- X.[j]+X[i,j])
    X.[j] <- X.[j]/100   
    X.. <- X..+X.[j]
    varX <- varX+X.[j]^2
}
X.. <- X../10
varX <- (varX-10*X..^2)/9
print(varX)
(elapsed.time2 <- proc.time()-start.time)

elapsed.time
elapsed.time2

# In R 1.8.1: 
 > elapsed.time
[1] 0.00 0.00 0.54   NA   NA
 > elapsed.time2
[1] 0.05 0.00 1.73   NA   NA
 >
# In S-_lus 8.2: 
 > elapsed.time
[1] 0.04 0.00 0.05 0.00 0.00
 > elapsed.time2
[1] 0.37 0.06 0.44 0.00 0.00

      hope this helps.  spencer graves

Uwe Ligges wrote:

> Raphael Schoenle wrote:
>
>> Hi,
>>  
>> How can I assign memory in R? My simulations are very slow.
>
>
> What do you mean with "assign memory in R"?
> You might want to optimize your code...
>
> Uwe Ligges
>
>
>> Thanks!,
>>  
>> -R
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! 
>> http://www.R-project.org/posting-guide.html
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list