<?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>datenum &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/datenum/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 04 May 2011 18:00:34 +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>Datenum performance</title>
		<link>https://undocumentedmatlab.com/articles/datenum-performance?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=datenum-performance</link>
					<comments>https://undocumentedmatlab.com/articles/datenum-performance#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 04 May 2011 18:00:34 +0000</pubDate>
				<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Undocumented function]]></category>
		<category><![CDATA[datenum]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Profiler]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2299</guid>

					<description><![CDATA[<p>The performance of the built-in Matlab function datenum can be significantly improved by using an undocumented internal help function</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/datenum-performance">Datenum performance</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/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>
<li><a href="https://undocumentedmatlab.com/articles/improving-save-performance" rel="bookmark" title="Improving save performance">Improving save performance </a> <small>There are many different ways of improving Matlab's standard save function performance. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties" rel="bookmark" title="Performance: accessing handle properties">Performance: accessing handle properties </a> <small>Handle object property access (get/set) performance can be significantly improved using dot-notation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plot-performance" rel="bookmark" title="Plot performance">Plot performance </a> <small>Undocumented inner plot mechanisms can significantly improve plotting performance ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A few days ago, a reader on StackOverflow <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5818583/faster-function-than-datenum-in-matlab">asked</a> whether it is possible to improve the performance of Matlab&#8217;s built-in <i><b>datenum</b></i> function. This question reminded me of a similar case that I answered exactly two years ago, of <a target="_blank" href="/articles/ismembc-undocumented-helper-function/">improving the performance of the built-in <i><b>ismember</b></i> function</a>.<br />
In both cases, the solution to the performance question can be found by simply using Matlab&#8217;s built-in profiler in order to extract just the core processing functionality. It is often found that in a particular situation there is no need for all the input arguments data validity checks, and under some known limitations we can indeed use the core functionality directly.<br />
In the case of <i><b>ismember</b></i>, it turned out that if we are assured in advance that the input data are sorted non-sparse non-NaN values, then we can use the undocumented built-in helper functions <i><b>ismembc</b></i> or <i><b>ismembc2</b></i> for much-improved performance over the standard <i><b>ismember</b></i>. Both <i><b>ismembc</b></i> and <i><b>ismembc2</b></i> happen to be mex files, although this is not always the case for helper functions.<br />
Our <i><b>datenum</b></i> case is very similar. It turns out that <i><b>datenum</b></i> uses the undocumented built-in helper function <i><b>dtstr2dtnummx</b></i> for the actual processing &#8211; converting a date from text to floating-point number. As I noted in <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5818583/faster-function-than-datenum-in-matlab/5823278#5823278">my response</a> to the StackOverflow question, we can directly use this helper function for improved performance: On my particular computer, <i><b>dtstr2dtnummx</b></i> is over 3 times faster than the standard <i><b>datenum</b></i> function:</p>
<pre lang='matlab'>
% Fast - using dtstr2dtnummx
>> tic, for i=1:1000; dateNum=dtstr2dtnummx({'2010-12-12 12:21:12.123'},'yyyy-MM-dd HH:mm:ss'); end; dateNum,toc
dateNum =
          734484.514722222
Elapsed time is 0.218423 seconds.
% Slower - using datenum
>> tic, for i=1:1000; dateNum=datenum({'2010-12-12 12:21:12.123'},'yyyy-mm-dd HH:MM:SS'); end; dateNum,toc
dateNum =
          734484.514722222   % Same value as dtstr2dtnummx - good!
Elapsed time is 0.658352 seconds.   % 3x slower than dtstr2dtnummx - bad!
</pre>
<p>While the difference in timing may appear negligible, if you are using this function to parse a text file with thousands of lines, each with its own timestamp, then these seemingly negligible time differences quickly add up. Of course, this only makes sense to do if you find out (using the profiler again) that this date parsing is a performance hotspot in your particular application. It was indeed such a performance hotspot in one of my applications, as it apparently was also for the original poster on StackOverflow.<br />
Like <i><b>ismembc</b></i>, <i><b>dtstr2dtnummx</b></i> is an internal mex function. On my Windows system it is located in C:\Program Files\Matlab\R2011a\toolbox\matlab\timefun\private\dtstr2dtnummx.mexw32. It will have a different extension non-Windows systems, but you will easily find it in its containing folder.<br />
To gain access to <i><b>dtstr2dtnummx</b></i>, simply add its folder to the Matlab path using the <i><b>addpath</b></i> function, or copy the dtstr2dtnummx.mexw32 file to another folder that is already on your Matlab path.<br />
Note that the string format is different between <i><b>dtstr2dtnummx</b></i> and <i><b>datenum</b></i>: In the test case above, <i><b>dtstr2dtnummx</b></i> used <code>'yyyy-MM-dd HH:mm:ss'</code>, while <i><b>datenum</b></i> required <code>'yyyy-<b>mm</b>-dd HH:<b>MM:SS</b>'</code>. I have no idea why MathWorks did not keep consistent formatting strings. But because of this, we need to be extra careful (<a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5831563/matlab-date-format/">example1</a>, <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5880242/matlab-datenum-generation">example2</a>). If you are interested in finding out how the <i><b>datenum</b></i> format strings translates into a <i><b>dtstr2dtnummx</b></i>, take a look at the helper function <i><b>cnv2icudf</b></i>, which is a very readable m-file located in the same folder as <i><b>dtstr2dtnummx</b></i>.<br />
To those interested, the folder that contains <i><b>dtstr2dtnummx</b></i> also contains some other interesting date conversion functions, so explore and enjoy!<br />
Perhaps the main lesson that can be learned from this article, and its <i><b>ismembc</b></i> predecessor of two years ago, is that it is very useful to profile the code for performance hotspots. When such a hotspot is found, don&#8217;t stop your profiling at the built-in Matlab functions &#8211; keep digging in the profiler results and perhaps you&#8217;ll find that you can improve performance by taking an internal shortcut.<br />
Have you discovered any other performance shortcuts in a built-in Matlab function? If so, please <a href="/articles/datenum-performance/#respond">post a comment</a> to tell us all about it.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/datenum-performance">Datenum performance</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/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>
<li><a href="https://undocumentedmatlab.com/articles/improving-save-performance" rel="bookmark" title="Improving save performance">Improving save performance </a> <small>There are many different ways of improving Matlab's standard save function performance. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties" rel="bookmark" title="Performance: accessing handle properties">Performance: accessing handle properties </a> <small>Handle object property access (get/set) performance can be significantly improved using dot-notation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plot-performance" rel="bookmark" title="Plot performance">Plot performance </a> <small>Undocumented inner plot mechanisms can significantly improve plotting performance ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/datenum-performance/feed</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
	</channel>
</rss>
