Dear R-helpers: I am doing some exploratory programming and am considering a routine that has several other routines defined within it, so that I can avoid a large and messy global re-programming to avoid naming conflicts. My question is this: Because it is interpreted, does R have to re-build these internal routines every time the new routine is called? I'm not overly worried right now about speed, but since my test cases currently run for several minutes (which is a long time to me) I don't want to learn a lesson the hard way that you, kind readers, might help me avoid. Thanks. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax:? 614-455-3265 http://www.StatisticalEngineering.com
On 4/21/2006 10:45 AM, Charles Annis, P.E. wrote:> Dear R-helpers: > > I am doing some exploratory programming and am considering a routine that > has several other routines defined within it, so that I can avoid a large > and messy global re-programming to avoid naming conflicts. > > My question is this: Because it is interpreted, does R have to re-build > these internal routines every time the new routine is called? I'm not > overly worried right now about speed, but since my test cases currently run > for several minutes (which is a long time to me) I don't want to learn a > lesson the hard way that you, kind readers, might help me avoid.I don't know the exact breakdown of the timing, but much of the work is done only once, when the source is parsed. At that point the outer function will be created in a structure parts of which are more or less identical to the structure of the functions that it creates within it. Then when it executes, copies need to be made and wrapped up as objects of their own. Only the latter work needs to be repeated on every call. I'd say avoiding naming conflicts is probably not a good enough reason on its own to use nested functions. You're better off putting your code in a package with a NAMESPACE if that's your goal. The main reason to use nested functions is for clarity: if some part of the work of a big function is neatly encapsulated in a small nested function, then do it. Those nested functions have access to the evaluation environment of their enclosing function. There are other reasons for nested functions (e.g. to create functions with static storage), but they are less common than doing it just to make the code clear. Duncan Murdoch
On Fri, 21 Apr 2006, Charles Annis, P.E. wrote:> Dear R-helpers: > > I am doing some exploratory programming and am considering a routine that > has several other routines defined within it, so that I can avoid a large > and messy global re-programming to avoid naming conflicts. > > My question is this: Because it is interpreted, does R have to re-build > these internal routines every time the new routine is called? I'm not > overly worried right now about speed, but since my test cases currently run > for several minutes (which is a long time to me) I don't want to learn a > lesson the hard way that you, kind readers, might help me avoid.Yes and no. The code will be parsed once, but the functions will be created each time the routine is called. This results in some extra memory turnover, but is almost certainly not significant in the grand scheme of things. Another approach to avoiding name conflicts is to put your code in a package with a namespace and export only the functions you want to be visible externally. You might want to run your code under the profiler (?Rprof) to find out where it is really spending all its time. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
"Charles Annis, P.E." <Charles.Annis at StatisticalEngineering.com> writes:> Dear R-helpers: > > I am doing some exploratory programming and am considering a routine that > has several other routines defined within it, so that I can avoid a large > and messy global re-programming to avoid naming conflicts. > > My question is this: Because it is interpreted, does R have to re-build > these internal routines every time the new routine is called?If you mean: f <- function(x) { f1 <- function(y) {...} f2 <- function(y) {...} f3 <- function(y) {...} f1(x) + f2(x) + f3(x) } Then, yes, as I understand it, each call to f() will include the overhead of defining functions f1, f2, and f3. In most cases, I would expect this overhead to be quite small in relation to the actual computations you are doing. You can probably use Rprof() to confirm this. You can also look at local() which I think provides a similar local namespace, but would not require redefinition of the helper functions. Here is a small example: f <- local({ f1 <- function(y) 2*y f2 <- function(y) y + 10 function(x) { f1(x) + f2(x) } }) + seth
Many thanks to Duncan Murdoch, Thomas Lumley, Patrick Burns, and Seth Falcon, for the illuminating advice, which will be found in the R-help archives. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Charles Annis, P.E. Sent: Friday, April 21, 2006 10:46 AM To: R-help at stat.math.ethz.ch Subject: [R] programming advice? Dear R-helpers: I am doing some exploratory programming and am considering a routine that has several other routines defined within it, so that I can avoid a large and messy global re-programming to avoid naming conflicts. My question is this: Because it is interpreted, does R have to re-build these internal routines every time the new routine is called? I'm not overly worried right now about speed, but since my test cases currently run for several minutes (which is a long time to me) I don't want to learn a lesson the hard way that you, kind readers, might help me avoid. Thanks. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax:? 614-455-3265 http://www.StatisticalEngineering.com ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html