[R] R function calling: efficiency of different alternatives

Douglas Bates bates at stat.wisc.edu
Tue Apr 1 16:12:38 CEST 2003


Ramon Diaz <rdiaz at cnio.es> writes:

> Dear all,
> 
> I have a piece of code, call it "FA", that will be called thousands of times 
> in a typical run of function "FB". I can:
> 
> a) define FA as a function outside of FB (in the global environment), and call 
> it;
> b) define FA as a function inside the body of FB and call it;
> c) "expand inline" FA inside FB.
> 
> FA mainly does data frame subsetting, runs svd's, and calls compiled C++ code.
> 
> I think I recall reading something about differences in efficiency between 
> those three approaches, but I can't find the information (I've searched the 
> email archives, Venables & Ripley's "S programming" and MASS, the R manuals, 
> and Burn's "S Poetry"). 
> 
> Are there any real performance differences? 
> 
> (I'd personally prefer b) since these functions will hopefully become a 
> contributed package, and FA is not supposed to be used directly by an end 
> user. I am aware of scoping differences between the three approaches, but 
> that is not my main concern now).

After R-1.7.0 is released you can use a namespace to make FA local to
your package.  

As Luke Tierney said in his presentation at DSC-2003, people have in
the past written functions in the style of b) primarily to keep the
names of local utility functions hidden.  Style a) and the use of
namespaces is a better way of accomplishing this.

I agree with what Barry Rowlingson wrote in his reply.  The best
approach is to write the code in the most convenient and
understandable way then profile the execution of the code to see where
the bottlenecks really are.



More information about the R-help mailing list