[R] Memory management

yoooooo magno_yu at ml.com
Wed Apr 11 00:16:57 CEST 2007

Hi all, I'm just curious how memory management works in R... I need to run an
optimization that keeps calling the same function with a large set of
parameters... so then I start to wonder if it's better if I attach the
variables first vs passing them in (coz that involves a lot of copying.. )

Thus, I do this
fn3 <- function(x, y, z, a, b, c){ sum(x, y, z, a, b, c) }
fn4 <- function(){ sum(x, y, z, a, b, c) }

rdn <- rep(1.1, times=1e8)
r <- proc.time()
for (i in 1:5)
  fn3(rdn, rdn, rdn, rdn, rdn, rdn)
time1 <- proc.time() - r

lt <- list(x = rdn, y = rdn, z = rdn, a = rdn, b = rdn, c = rdn)
r <- proc.time()
for (i in 1:5)
time2 <- proc.time() - r

The output is
[1] 25.691  0.003 25.735  0.000  0.000
[1] 25.822  0.005 25.860  0.000  0.000

Turns out attaching takes longer to run.. which is counter intuitive (unless
the search to the pos=2 envir takes long time as well) Do you guys know why
this is the case? 
View this message in context: http://www.nabble.com/Memory-management-tf3556238.html#a9929835
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list