<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cellfun &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/cellfun/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Mon, 11 May 2009 22:28:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>cellfun &#8211; undocumented performance boost</title>
		<link>https://undocumentedmatlab.com/articles/cellfun-undocumented-performance-boost?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cellfun-undocumented-performance-boost</link>
					<comments>https://undocumentedmatlab.com/articles/cellfun-undocumented-performance-boost#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 11 May 2009 22:28:47 +0000</pubDate>
				<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[cellfun]]></category>
		<category><![CDATA[Performance]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=260</guid>

					<description><![CDATA[<p>Matlab's built-in cellfun function has an undocumented option to significantly improve performance in some cases.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/cellfun-undocumented-performance-boost">cellfun &#8211; undocumented performance boost</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/datestr-performance" rel="bookmark" title="datestr performance">datestr performance </a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/hgs-undocumented-parameters-interface" rel="bookmark" title="HG&#039;s undocumented parameters interface">HG&#039;s undocumented parameters interface </a> <small>Some HG functions also accept inputs parameters in a struct fields rather than the normal P-V pairs format. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/sprintfc-undocumented-helper-function" rel="bookmark" title="sprintfc &#8211; undocumented helper function">sprintfc &#8211; undocumented helper function </a> <small>The built-in sprintfc function can be used to quickly generate a cell-array of formatted strings. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/performance-scatter-vs-line" rel="bookmark" title="Performance: scatter vs. line">Performance: scatter vs. line </a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab&#8217;s built-in <a rel="nofollow" target="_blank" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cellfun.html"><em><strong>cellfun</strong></em></a> function has traditionally enabled several named (string) processing functions such as &#8216;isempty&#8217;. The relevant code would look like this:</p>
<blockquote>
<pre><span style="color: #000000">data = cellfun(</span><span style="color: #800080">'isempty'</span><span style="color: #000000">,cellArray);</span></pre>
</blockquote>
<p>In recent years, newer Matlab releases has added support for function handles, so the previous code snippet can now be written as follows:</p>
<blockquote>
<pre><span style="color: #000000">data = cellfun(@isempty,cellArray);</span></pre>
</blockquote>
<p>The newer function-handle format is &#8220;cleaner&#8221; and more extensive than the former format, accepting any function, not just the limited list of pre-specified processing function names (&#8216;isreal&#8217;, &#8216;islogical&#8217;, &#8216;length&#8217;, &#8216;ndims&#8217;, &#8216;prodofsize&#8217;). Some have even reported that the older format has limitations vis-a-vis compilation etc.<br />
All this is well known and documented. However, it turns out that, counter-intuitively (and undocumented), the older format is actually much <strong>faster</strong> than the newer format for those pre-specified processing function names. The reason appears to be that &#8216;isempty&#8217; (as well as the other predefined string functions) uses specific code-branches optimized for performance:</p>
<blockquote>
<pre><span style="color: #000000">&gt;&gt; c = mat2cell(1:1e6,1,repmat(1,1,1e6));
&gt;&gt; tic, d=cellfun(</span><span style="color: #800080">'isempty'</span><span style="color: #000000">,c); toc</span>
Elapsed time is 0.115583 seconds.
<span style="color: #000000">&gt;&gt; tic, d=cellfun(@isempty,c); toc</span>
Elapsed time is 7.493989 seconds.</pre>
</blockquote>
<p>Perhaps a future Matlab release will improve cellfun&#8217;s internal code, to check for function-handle equality to the optimized functions, and use the optimized code branch if possible. When I posted this issue today as a correction to a reader&#8217;s misconception, Matlab&#8217;s Loren Shure <a rel="nofollow" href="http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/#comment-30304" target="_blank">commented as follows</a>:<br />
<q>We could improve cellfun to check function handles to see if they match specified strings. Even then MATLAB would have to be careful in case the user has overridden the built-in version of whatever the string points to.</q><br />
While this comment seems to imply that the performance boost feature will be maintained and possibly improved in future releases, users should note that this is not guarantied. One could even argue that future code optimizations would be applied to the new (function-handle) format rather than the old string format. The performance pendulum might also change based on user platform. Therefore, users for whom performance is critical should always test both versions  on their target system: &#8216;isempty&#8217; vs. @isempty etc. (note that the corresponding function for &#8216;prodofsize&#8217; is @numel).</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/cellfun-undocumented-performance-boost">cellfun &#8211; undocumented performance boost</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/datestr-performance" rel="bookmark" title="datestr performance">datestr performance </a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/hgs-undocumented-parameters-interface" rel="bookmark" title="HG&#039;s undocumented parameters interface">HG&#039;s undocumented parameters interface </a> <small>Some HG functions also accept inputs parameters in a struct fields rather than the normal P-V pairs format. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/sprintfc-undocumented-helper-function" rel="bookmark" title="sprintfc &#8211; undocumented helper function">sprintfc &#8211; undocumented helper function </a> <small>The built-in sprintfc function can be used to quickly generate a cell-array of formatted strings. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/performance-scatter-vs-line" rel="bookmark" title="Performance: scatter vs. line">Performance: scatter vs. line </a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/cellfun-undocumented-performance-boost/feed</wfw:commentRss>
			<slash:comments>18</slash:comments>
		
		
			</item>
	</channel>
</rss>
