<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Use nested functions to memoize costly functions</title>
	<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/</link>
	<description>Loren Shure  works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics. </description>
	<pubDate>Fri, 25 Jul 2008 11:11:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-28325</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 17 Apr 2008 01:40:47 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-28325</guid>
		<description>Yi-

The rest of the information is also stored in the mat-file.  What you are seeing is the size of the function handle itself, not everything it encapsulates.

As for pre-allocation, that would be nice, and it might help performance, especially if I chunked it up - but it can't solve everything as we don't know how big to make it.  Whether or not that slows things down materially depends on the very picky details of how it gets called and how much and how often the array is grown.

For the example, it was going to make things muddier and harder to understand, I think, to write the chunked memory version.

--Loren</description>
		<content:encoded><![CDATA[<p>Yi-</p>
<p>The rest of the information is also stored in the mat-file.  What you are seeing is the size of the function handle itself, not everything it encapsulates.</p>
<p>As for pre-allocation, that would be nice, and it might help performance, especially if I chunked it up - but it can&#8217;t solve everything as we don&#8217;t know how big to make it.  Whether or not that slows things down materially depends on the very picky details of how it gets called and how much and how often the array is grown.</p>
<p>For the example, it was going to make things muddier and harder to understand, I think, to write the chunked memory version.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yi Cao</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-28319</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Wed, 16 Apr 2008 20:28:37 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-28319</guid>
		<description>Loren

It is an interesting usage of nested function. Two questions:

1. After complete all calls and save f to a mat file, then clear whole workspace. Now, load the mat file and check with whos, the function handle, f only occupies 16 bytes. But check with s=functions(f), and s.workspace{1} all information is still there. I worder where the information is stored?

2. In the inner function, x and y are not pre-allocated. Will this affect the performance when the sizes of x and y becomes large?

-Yi</description>
		<content:encoded><![CDATA[<p>Loren</p>
<p>It is an interesting usage of nested function. Two questions:</p>
<p>1. After complete all calls and save f to a mat file, then clear whole workspace. Now, load the mat file and check with whos, the function handle, f only occupies 16 bytes. But check with s=functions(f), and s.workspace{1} all information is still there. I worder where the information is stored?</p>
<p>2. In the inner function, x and y are not pre-allocated. Will this affect the performance when the sizes of x and y becomes large?</p>
<p>-Yi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-27088</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 04 Mar 2008 18:28:07 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-27088</guid>
		<description>David-

inner itself doesn't retain the values.  The function handle to the nested function does.  All related get cleared up when the last reference to the nested function is cleared.  However, local variables in inner get cleared right after each time it is run.  Only x and y are retained with the nested function handle.

--Loren</description>
		<content:encoded><![CDATA[<p>David-</p>
<p>inner itself doesn&#8217;t retain the values.  The function handle to the nested function does.  All related get cleared up when the last reference to the nested function is cleared.  However, local variables in inner get cleared right after each time it is run.  Only x and y are retained with the nested function handle.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-27087</link>
		<dc:creator>David</dc:creator>
		<pubDate>Tue, 04 Mar 2008 18:24:49 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-27087</guid>
		<description>Hi,
I was just wondering: the function 'inner' retains the variable values between runs. When does memory allocated by it get freed?</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I was just wondering: the function &#8216;inner&#8217; retains the variable values between runs. When does memory allocated by it get freed?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-20665</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 07 Nov 2007 20:25:34 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-20665</guid>
		<description>Ged-

Performance aside (though it is a legitimate question), persistent doesn't easily allow multiple instantiations, for example, 2 gui screens, while nested functions allow this naturally. 

--Loren</description>
		<content:encoded><![CDATA[<p>Ged-</p>
<p>Performance aside (though it is a legitimate question), persistent doesn&#8217;t easily allow multiple instantiations, for example, 2 gui screens, while nested functions allow this naturally. </p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ged</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-20663</link>
		<dc:creator>Ged</dc:creator>
		<pubDate>Wed, 07 Nov 2007 20:21:48 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-20663</guid>
		<description>Hello again,

Had a quick look at this myself... It seems that with largish arrays (e.g. 1000x1000), the options are much the same, but that with smaller arrays (e.g. 100x100) persistent is slower than nested, with the persistent declaration line itself being one of the slower lines in the profile.

I can't think of any good reasons for this, but it looks to me like nested beats persistent in any case, as well as allowing you to have multiple instances.

Hope that's of interest to someone (and that maybe Loren or someone else can suggest reasons why persistent isn't better)

Ged</description>
		<content:encoded><![CDATA[<p>Hello again,</p>
<p>Had a quick look at this myself&#8230; It seems that with largish arrays (e.g. 1000&#215;1000), the options are much the same, but that with smaller arrays (e.g. 100&#215;100) persistent is slower than nested, with the persistent declaration line itself being one of the slower lines in the profile.</p>
<p>I can&#8217;t think of any good reasons for this, but it looks to me like nested beats persistent in any case, as well as allowing you to have multiple instances.</p>
<p>Hope that&#8217;s of interest to someone (and that maybe Loren or someone else can suggest reasons why persistent isn&#8217;t better)</p>
<p>Ged</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ged</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-19836</link>
		<dc:creator>Ged</dc:creator>
		<pubDate>Thu, 01 Nov 2007 11:41:44 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-19836</guid>
		<description>Hi Loren,

&#62; With persistent, you only have one instance

This is an obvious advantage for nested functions when multiple copies are useful, but how do the two options compare when both are feasible? In terms of speed, etc?

Instead of memoising, I have lots of data which I run a computationally intensive function on, parts of which are invariant to some aspects of the particular data. I can use either persistent variables or nested functions to store partial results for the invariant parts, speeding up the function for further calls on remaining data. Alternatively, I could use other functions to compute the invariant parts and simply pass these in to a different function.

I expected the first option (persistence) to be much quicker than the third (passing) since no memory needs to be copied back and forth, but I note on profiling that the persistent line itself seems to be quite a slow part of the code (when repeated over many data). I hadn't considered the second option (nesting) at all; I wonder if it might be quicker than persistence... If so, why? Persistent arrays seem like a concept that should map efficiently to the lower level implementation, while nested functions keeping track of their own workspaces sounds more complex.

Many thanks,
Ged</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>&gt; With persistent, you only have one instance</p>
<p>This is an obvious advantage for nested functions when multiple copies are useful, but how do the two options compare when both are feasible? In terms of speed, etc?</p>
<p>Instead of memoising, I have lots of data which I run a computationally intensive function on, parts of which are invariant to some aspects of the particular data. I can use either persistent variables or nested functions to store partial results for the invariant parts, speeding up the function for further calls on remaining data. Alternatively, I could use other functions to compute the invariant parts and simply pass these in to a different function.</p>
<p>I expected the first option (persistence) to be much quicker than the third (passing) since no memory needs to be copied back and forth, but I note on profiling that the persistent line itself seems to be quite a slow part of the code (when repeated over many data). I hadn&#8217;t considered the second option (nesting) at all; I wonder if it might be quicker than persistence&#8230; If so, why? Persistent arrays seem like a concept that should map efficiently to the lower level implementation, while nested functions keeping track of their own workspaces sounds more complex.</p>
<p>Many thanks,<br />
Ged</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-16298</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Fri, 22 Jun 2007 16:32:14 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-16298</guid>
		<description>Hi Loren,
Calling nested functions which access cell arrays seems to take time proportional to the size of the array, while nested functions using numeric arrays do not.  I asked about this in the newsgroup (thread "matlab and copy-on-write" -- I thought it was due to a memory copy, but that seems to be incorrect) but have yet to find an explanation or work-around.  Any ideas?</description>
		<content:encoded><![CDATA[<p>Hi Loren,<br />
Calling nested functions which access cell arrays seems to take time proportional to the size of the array, while nested functions using numeric arrays do not.  I asked about this in the newsgroup (thread &#8220;matlab and copy-on-write&#8221; &#8212; I thought it was due to a memory copy, but that seems to be incorrect) but have yet to find an explanation or work-around.  Any ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Markus</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-153</link>
		<dc:creator>Markus</dc:creator>
		<pubDate>Thu, 27 Apr 2006 07:09:16 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-153</guid>
		<description>The winning contribution of the last Matlab programming contest is a bad example for using nested functions. You can see the code at http://www.mathworks.com/contest/blockbuster.cgi/view_submission.html?id=31864 or a similar unobfuscated version at http://www.mathworks.com/contest/blockbuster.cgi/view_submission.html?id=31890. The functions solveri and CalculateMoves are nested functions (this is better visible after correct identing). Both functions are quite large and use quite a number of variables. After starting to modify the code I quickly ran into the problem that I erroneously used variables twice and the code did not work any more.

The use of nested functions requires a lot of discipline and due to that is very error-prone, especially when different people work on the same code.</description>
		<content:encoded><![CDATA[<p>The winning contribution of the last Matlab programming contest is a bad example for using nested functions. You can see the code at <a href="http://www.mathworks.com/contest/blockbuster.cgi/view_submission.html?id=31864" rel="nofollow">http://www.mathworks.com/contest/blockbuster.cgi/view_submission.html?id=31864</a> or a similar unobfuscated version at <a href="http://www.mathworks.com/contest/blockbuster.cgi/view_submission.html?id=31890." rel="nofollow">http://www.mathworks.com/contest/blockbuster.cgi/view_submission.html?id=31890.</a> The functions solveri and CalculateMoves are nested functions (this is better visible after correct identing). Both functions are quite large and use quite a number of variables. After starting to modify the code I quickly ran into the problem that I erroneously used variables twice and the code did not work any more.</p>
<p>The use of nested functions requires a lot of discipline and due to that is very error-prone, especially when different people work on the same code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-83</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 16 Mar 2006 12:50:22 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-83</guid>
		<description>You are seeing effects of numerical roundoff.  I recommend you read some of the faqs on the web.  You can start off &lt;a href="http://www.mit.edu/~pwb/cssm/matlab-faq.html#eps" rel="nofollow"&gt;here&lt;/a&gt; or with &lt;a href="http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf" rel="nofollow"&gt;this article&lt;/a&gt; by Cleve.</description>
		<content:encoded><![CDATA[<p>You are seeing effects of numerical roundoff.  I recommend you read some of the faqs on the web.  You can start off <a href="http://www.mit.edu/~pwb/cssm/matlab-faq.html#eps" rel="nofollow">here</a> or with <a href="http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf" rel="nofollow">this article</a> by Cleve.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
