I am developing an R package for internal use, and eventually for public release. My understanding is that there is no easy way to avoid copying function arguments in R (i.e. we don't have the concept of pointers in R), which makes me wary of freely creating chains of function calls since each function call implies data copy overhead. Is the above assessment fair? Are there any good write-ups on best practices for writing efficient R libraries that take into consideration the above-mentioned limitations, and any others that might exist? Thank you, Alireza -- View this message in context: http://r.789695.n4.nabble.com/Best-practices-for-writing-R-functions-tp3686674p3686674.html Sent from the R devel mailing list archive at Nabble.com.
From my personal experience and following this list some for a few years, the best practice is initially to ignore the compute time question, because the cost of your time getting it to do what you want is far greater, at least initially. Don't worry about compute time until it becomes an issue. When it does, the standard advice I've seen on this list is to experiment with different ways of writing the same thing in R, guided by "profiling R code", as described in the "Writing R Extensions" manual. (Googling for "profiling R code" identified examples.) Hope this helps. Spencer Graves On 7/22/2011 6:26 AM, Alireza Mahani wrote:> I am developing an R package for internal use, and eventually for public > release. My understanding is that there is no easy way to avoid copying > function arguments in R (i.e. we don't have the concept of pointers in R), > which makes me wary of freely creating chains of function calls since each > function call implies data copy overhead. > > Is the above assessment fair? Are there any good write-ups on best practices > for writing efficient R libraries that take into consideration the > above-mentioned limitations, and any others that might exist? > > Thank you, > Alireza > > > -- > View this message in context: http://r.789695.n4.nabble.com/Best-practices-for-writing-R-functions-tp3686674p3686674.html > Sent from the R devel mailing list archive at Nabble.com. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
On Jul 22, 2011, at 9:26 AM, Alireza Mahani wrote:> I am developing an R package for internal use, and eventually for public > release. My understanding is that there is no easy way to avoid copying > function arguments in R (i.e. we don't have the concept of pointers in R), > which makes me wary of freely creating chains of function calls since each > function call implies data copy overhead. > > Is the above assessment fair?No. Although R maintains the illusion of pass-by-value, it does to considerable length to prevent copying if not needed:> r=rnorm(1e6) > tracemem(r)[1] "<0x103fa2000>"> f = function(x) x > invisible(f(f(f(f(f(f(r)))))))As you see not a single copy is created even though there are 6 function calls. As Spencer said, you should not worry unless you see a sign of a problem. Cheers, Simon> Are there any good write-ups on best practices > for writing efficient R libraries that take into consideration the > above-mentioned limitations, and any others that might exist? > > Thank you, > Alireza > > > -- > View this message in context: http://r.789695.n4.nabble.com/Best-practices-for-writing-R-functions-tp3686674p3686674.html > Sent from the R devel mailing list archive at Nabble.com. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >
Apparently Analagous Threads
- Best practices for writing R functions (really copying)
- How to safely using OpenMP pragma inside a .C() function?
- Performance of .C and .Call functions vs. native R code
- Is it possible to pass a function argument from R to compiled code in C?
- Assigning a new name to object loaded with "load()"