Skip to content

for loop performance

1 message · Barth B. Riley

#
Thanks Martin,  this is very helpful.
Barth

-----Original Message-----
From: Martin Morgan [mailto:mtmorgan at fhcrc.org]
Sent: Thursday, April 14, 2011 11:04 AM
To: Barth B. Riley
Subject: Re: [R] for loop performance
On 04/14/2011 07:12 AM, Barth B. Riley wrote:
The answer depends on what the function returns, and perhaps other things. For instance

f = function() { a = 1; b = 2; a }
x = f()

'a' and 'b' created inside f() will be garbage collected; there is no way to access their value after f() returns. But in

g = function() { a = 1; function() {} }
y = g()

(i.e., g a function that returns a function) y is now a function, functions have environments (the one in which they were created), and the environment can accessed, so 'a' is still accessible

 > environment(y)[["a"]]
[1] 1

and is not available for garbage collection until y is removed.

Martin
--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.