Hi there, I have just found that the ``attach'' function can get you into trouble when called many times. For example, you have a simulation routine called ``f()'', in which you used ``attach'' and no corresponding ``detach''. Then you call this function many times. You will find that the system performance get slower and slower, because you are making the R search path longer and longer. So be careful when you use attach in a function! Below is a demonstration of this performance loss, you will see a linear growth in CPU time usage. Adding a ``detach()'' call at the end of ``f'' will get rid of this problem. ############################### f <- function(){ theta <- list(one=2.0, two=0.3, three=0.4) attach(theta) x <- c(one, two, three) sample(x, 1) } test <- function(n=400){ timeu <- numeric(n) for(i in seq(n)){ timeu[i] <- system.time({ resi <- f() })[3] } plot(timeu) } test() ############################## ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Li Dongfeng ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ldf-nospacm at math.pku.edu.cn ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2004-02-24
On Tue, 2004-02-24 at 09:32, li dongfeng wrote:> Hi there, > > I have just found that the ``attach'' function > can get you into trouble when called many times. > For example, you have a simulation routine called ``f()'', > in which you used ``attach'' and no corresponding ``detach''. > Then you call this function many times. You will find that > the system performance get slower and slower, > because you are making the R search path longer and longer. > So be careful when you use attach in a function! > > Below is a demonstration of this performance loss, > you will see a linear growth in CPU time usage. > Adding a ``detach()'' call at the end of ``f'' > will get rid of this problem. > > ############################### > f <- function(){ > theta <- list(one=2.0, two=0.3, three=0.4) > attach(theta) > x <- c(one, two, three) > sample(x, 1) > } > > test <- function(n=400){ > timeu <- numeric(n) > for(i in seq(n)){ > timeu[i] <- > system.time({ > resi <- f() > })[3] > } > plot(timeu) > } > test() > ##############################A better general and more efficient solution, without getting into the details of the above functions, would be to use with() instead of attach()/detach(). Change your function 'f' to: f <- function(){ theta <- list(one = 2.0, two = 0.3, three = 0.4) x <- with(theta, c(one, two, three)) sample(x, 1) } Now run test() using with() versus attach()/detach() and note the time savings. Even with the detach() it is not "efficient". For example using: f <- function(){ theta <- list(one = 2.0, two = 0.3, three = 0.4) attach(theta) x <- c(one, two, three) sample(x, 1) detach(theta) }> system.time(test())[1] 38.54 0.02 38.92 0.00 0.00 versus using: f <- function(){ theta <- list(one = 2.0, two = 0.3, three = 0.4) x <- with(theta, c(one, two, three)) sample(x, 1) }> system.time(test())[1] 0.16 0.00 0.16 0.00 0.00 Those are on a 3.2 Ghz P4 Dell 5150 laptop with 2 GB of RAM for comparison. See ?with for more information. You will find relatively recent posts on this in the list archives as a preferred approach. HTH, Marc Schwartz
An alternative to attach is with: x <- with( theta, c(one,two,three) ) --- Date: Tue, 24 Feb 2004 23:32:0 +0800 From: li dongfeng <ldf at math.pku.edu.cn> To: r-help at stat.math.ethz.ch <r-help at stat.math.ethz.ch> Subject: [R] be careful: using attach in R functions Hi there, I have just found that the ``attach'' function can get you into trouble when called many times. For example, you have a simulation routine called ``f()'', in which you used ``attach'' and no corresponding ``detach''. Then you call this function many times. You will find that the system performance get slower and slower, because you are making the R search path longer and longer. So be careful when you use attach in a function! Below is a demonstration of this performance loss, you will see a linear growth in CPU time usage. Adding a ``detach()'' call at the end of ``f'' will get rid of this problem. ############################### f <- function(){ theta <- list(one=2.0, two=0.3, three=0.4) attach(theta) x <- c(one, two, three) sample(x, 1) } test <- function(n=400){ timeu <- numeric(n) for(i in seq(n)){ timeu[i] <- system.time({ resi <- f() })[3] } plot(timeu) } test() ############################## ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Li Dongfeng ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ldf-nospacm at math.pku.edu.cn ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2004-02-24
You can write your code more defensively. There are a few ways to do that: 1. Right after the attach(object), do on.exit(detach(object)). 2. If you know the function will be call repeatedly, it might be a good idea to check whether the object has been attached. This way you only need to attach once. I can think of a couple of way to do that. The simplest is probably to check and see if the object name appear in the search list. Andy> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of li dongfeng > Sent: Monday, February 23, 2004 1:00 PM > To: r-help at stat.math.ethz.ch > Subject: [R] be careful: using attach in R functions > > > Hi there, > > I have just found that the ``attach'' function > can get you into trouble when called many times. > For example, you have a simulation routine called ``f()'', > in which you used ``attach'' and no corresponding ``detach''. > Then you call this function many times. You will find that > the system performance get slower and slower, > because you are making the R search path longer and longer. > So be careful when you use attach in a function! > > Below is a demonstration of this performance loss, > you will see a linear growth in CPU time usage. > Adding a ``detach()'' call at the end of ``f'' > will get rid of this problem. > > ############################### > f <- function(){ > theta <- list(one=2.0, two=0.3, three=0.4) > attach(theta) > x <- c(one, two, three) > sample(x, 1) > } > > test <- function(n=400){ > timeu <- numeric(n) > for(i in seq(n)){ > timeu[i] <- > system.time({ > resi <- f() > })[3] > } > plot(timeu) > } > test() > ############################## > > > ????????Li Dongfeng > ????????ldf-nospacm at math.pku.edu.cn > ??????????2004-02-24 > > ______________________________________________ > 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 > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
li dongfeng wrote:> Hi there, > > I have just found that the ``attach'' function > can get you into trouble when called many times.[..]> Below is a demonstration of this performance loss, > you will see a linear growth in CPU time usage. > Adding a ``detach()'' call at the end of ``f'' > will get rid of this problem.Yes, this behaviour is well known (to me using attach for differential equation models long ago). Detaching ist absolutely required. As an alternative one can use the with() function. Thomas P.
li dongfeng wrote:> > Hi there, > > I have just found that the ``attach'' function > can get you into trouble when called many times. > For example, you have a simulation routine called ``f()'', > in which you used ``attach'' and no corresponding ``detach''.Well, attach() may be useful for convenient interactive data analysis. I'd never use it in a simulation, since you can access all objects without attaching anything. (Well, I suggest not to use it at all, at least to our students ... and I'm using it only in very rare cases myself.) Uwe Ligges> Then you call this function many times. You will find that > the system performance get slower and slower, > because you are making the R search path longer and longer. > So be careful when you use attach in a function! > > Below is a demonstration of this performance loss, > you will see a linear growth in CPU time usage. > Adding a ``detach()'' call at the end of ``f'' > will get rid of this problem. > > ############################### > f <- function(){ > theta <- list(one=2.0, two=0.3, three=0.4) > attach(theta) > x <- c(one, two, three) > sample(x, 1) > } > > test <- function(n=400){ > timeu <- numeric(n) > for(i in seq(n)){ > timeu[i] <- > system.time({ > resi <- f() > })[3] > } > plot(timeu) > } > test() > ############################## > > ????????????????Li Dongfeng > ????????????????ldf-nospacm at math.pku.edu.cn > ????????????????????2004-02-24 > > ______________________________________________ > 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
Using attach() in this context may not be wise. I tend to only use attach() when working interactively. It might be better to use with() in this situation, such as f <- function() { theta <- list(one=2.0, two=0.3, three=0.4) x <- with(theta, c(one, two, three)) sample(x, 1) } -roger li dongfeng wrote:> Hi there, > > I have just found that the ``attach'' function > can get you into trouble when called many times. > For example, you have a simulation routine called ``f()'', > in which you used ``attach'' and no corresponding ``detach''. > Then you call this function many times. You will find that > the system performance get slower and slower, > because you are making the R search path longer and longer. > So be careful when you use attach in a function! > > Below is a demonstration of this performance loss, > you will see a linear growth in CPU time usage. > Adding a ``detach()'' call at the end of ``f'' > will get rid of this problem. > > ############################### > f <- function(){ > theta <- list(one=2.0, two=0.3, three=0.4) > attach(theta) > x <- c(one, two, three) > sample(x, 1) > } > > test <- function(n=400){ > timeu <- numeric(n) > for(i in seq(n)){ > timeu[i] <- > system.time({ > resi <- f() > })[3] > } > plot(timeu) > } > test() > ############################## > > > ããããããããLi Dongfeng > ããããããããldf-nospacm at math.pku.edu.cn > ãããããããããã2004-02-24 > > ______________________________________________ > 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 >