[R] Generate temporary objects or not?
tlumley at u.washington.edu
Tue Feb 27 17:32:31 CET 2001
On Tue, 27 Feb 2001, Zsombor Cseres-Gergely wrote:
> Dear all,
> Playing with a toy problem, I wondered how much it costs to generate
> `internal' objects in a function that are not strictly needed. An example:
> mean(apply(t(matrix(as.vector(x) - x0 / h, , nrow=nrow(x), ncol=ncol(x))), c(2) epakern, dim)) / h^dim
> It would be more _readable_ if I defined the object inside the apply(), stick
> it there and rm() after, but my feeling is that creating it and than removing
> is costlier. (If the above structure is too simple, imagine something more
> complicated) Am I right, it does not matter, since it is created inside anyway?
The simple way to answer questions about which way is slower/more memory
intensive is to try it and see. Between Rprof(), unix.time() and gc(),
you have all the information you need. My guess is that it won't matter
There are two fundamental principles of optimisation
1) Don't do it unless you need it
2) Measure, don't guess, about speed.
You probably don't need the t(), though. You should just be able to change
the MARGIN argument to apply()
> Another question is if apply() could feed the data into the nonfirst
> argument of the function applied. I have clue just for the first.
You may need to define a wrapper function
apply(data,1, function(y) f(x,y))
will do f(x,data[1,]), f(x,data[2,]) and so on, if that's what you mean.
Thomas Lumley Asst. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help