[R] memory usage benefit from "anonymous" variable constructions.

Paul Johnson pauljohn32 at gmail.com
Sun Jun 3 20:00:14 CEST 2012


This is an "I was just wondering" question.

When the package "dataframe" was announced, the author claimed to
reduce the number of times a data frame was copied, I started to
wonder if I should care about this in my projects.  Has anybody
written a general guide for how to write R code that doesn't
needlessly exhaust RAM?

In Objective-C, we used to gain some considerable advantages by
avoiding declaring objects separately, using anonymous variable
instead. The storage was allocated on the stack, I think, and I think
there was talk that the numbers might stay 'closer' to the CPU
(register?) for immediate usage.

Does this benefit in R as well?  For example, instead of the way I
would usually do this:

 mf <- model.frame(model)
  y <- model.response(mf)

Here is the anonymous alternative, mf is never declared

y <- model.response(model.frame(model))

On the face of it, I can imagine this might be better because no
permanent thing "mf" is created, the garbage collector wouldn't be
called into play if all the data is local and disappears immediately.
But, then again, R is doing lots of stuff "under the hood" that I've
never bothered to learn about.


pj
-- 
Paul E. Johnson
Professor, Political Science    Assoc. Director
1541 Lilac Lane, Room 504     Center for Research Methods
University of Kansas               University of Kansas
http://pj.freefaculty.org            http://quant.ku.edu



More information about the R-help mailing list