<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831</link>
    <title>MATLAB Central Newsreader - matlab typecast function very slow</title>
    <description>Feed for thread: matlab typecast function very slow</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 03 Nov 2009 11:30:46 -0500</pubDate>
      <title>matlab typecast function very slow</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831#691685</link>
      <author>roger</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
I have a function that reads large binary files by reading a couple of&lt;br&gt;
numbers per time in a complicated loop using fread.&lt;br&gt;
&lt;br&gt;
Since the function isn't too fast I thought I'd read the entire file&lt;br&gt;
at once as uint8 and then typecast byte by byte to the relevant&lt;br&gt;
datatypes.&lt;br&gt;
&lt;br&gt;
To my surprise the typecast method is actually a _lot_ slower than&lt;br&gt;
consecutively using fread, on inspection the profiler shows that the&lt;br&gt;
typecast function is the main culprit.&lt;br&gt;
&lt;br&gt;
The function that serves the typecast.m function is a mex file called&lt;br&gt;
typecastc.mexw32 (on my 32 bit system that is). It seems to be a&lt;br&gt;
function which is compiled from an accompying c file that is located&lt;br&gt;
in the same private directory: typecastc.c&lt;br&gt;
&lt;br&gt;
When I look at the c source file I see that the function itself&lt;br&gt;
doesn't do any typecasting, it just creates a relevant datatype with&lt;br&gt;
the mex mxCreateNumericMatrix and then leaves the actual typecasting&lt;br&gt;
to the mx libraries.&lt;br&gt;
&lt;br&gt;
So all in all the typecasting slowness seems a result of matlab's&lt;br&gt;
inability to deal with c-style pointers from m-files and the penalty&lt;br&gt;
for this is the mx library overhead.&lt;br&gt;
&lt;br&gt;
An odd situation.&lt;br&gt;
&lt;br&gt;
Anyone have a better method (than the mathworks have ;) for reading/&lt;br&gt;
typecasting?&lt;br&gt;
&lt;br&gt;
best regards R</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 12:49:02 -0500</pubDate>
      <title>Re: matlab typecast function very slow</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831#691700</link>
      <author>Bruno Luong</author>
      <description>roger &amp;lt;northsolomonsea@gmail.com&amp;gt; wrote in message &amp;lt;05e24510-1633-4d87-9cbb-39230a6efeec@n35g2000yqm.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a function that reads large binary files by reading a couple of&lt;br&gt;
&amp;gt; numbers per time in a complicated loop using fread.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Since the function isn't too fast I thought I'd read the entire file&lt;br&gt;
&amp;gt; at once as uint8 and then typecast byte by byte to the relevant&lt;br&gt;
&amp;gt; datatypes.&lt;br&gt;
&lt;br&gt;
Why doing byte-by-byte? Use typecast for the whole array. Many function Matlab function is not designed to called many times, but rather on an array. Example:&lt;br&gt;
&lt;br&gt;
b=zeros(8*1024^2,1,'uint8');&lt;br&gt;
&lt;br&gt;
tic;&lt;br&gt;
d=typecast(b,'double');&lt;br&gt;
toc % Elapsed time is 0.012408 seconds.&lt;br&gt;
&lt;br&gt;
tic;&lt;br&gt;
% a already allocated&lt;br&gt;
k=1;&lt;br&gt;
for i=1:8:length(b)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d(k)=typecast(b(i+(0:7)),'double');&lt;br&gt;
end&lt;br&gt;
toc % 40 sec&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To my surprise the typecast method is actually a _lot_ slower than&lt;br&gt;
&amp;gt; consecutively using fread, on inspection the profiler shows that the&lt;br&gt;
&amp;gt; typecast function is the main culprit.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The function that serves the typecast.m function is a mex file called&lt;br&gt;
&amp;gt; typecastc.mexw32 (on my 32 bit system that is). It seems to be a&lt;br&gt;
&amp;gt; function which is compiled from an accompying c file that is located&lt;br&gt;
&amp;gt; in the same private directory: typecastc.c&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; When I look at the c source file I see that the function itself&lt;br&gt;
&amp;gt; doesn't do any typecasting, it just creates a relevant datatype with&lt;br&gt;
&amp;gt; the mex mxCreateNumericMatrix and then leaves the actual typecasting&lt;br&gt;
&amp;gt; to the mx libraries.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So all in all the typecasting slowness seems a result of matlab's&lt;br&gt;
&amp;gt; inability to deal with c-style pointers from m-files and the penalty&lt;br&gt;
&amp;gt; for this is the mx library overhead.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; An odd situation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Anyone have a better method (than the mathworks have ;) for reading/&lt;br&gt;
&amp;gt; typecasting?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Alternatively use MEMMAPFILE to read binary data.&lt;br&gt;
&lt;br&gt;
or MEX it, but first try to use TYPECAST on the whole array.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 13:18:39 -0500</pubDate>
      <title>Re: matlab typecast function very slow</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831#691704</link>
      <author>roger</author>
      <description>On Nov 3, 1:49&#160;pm, &quot;Bruno Luong&quot; &amp;lt;b.lu...@fogale.findmycountry&amp;gt; wrote:&lt;br&gt;
&amp;gt; roger &amp;lt;northsolomon...@gmail.com&amp;gt; wrote in message &amp;lt;05e24510-1633-4d87-9cbb-39230a6ef...@n35g2000yqm.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I have a function that reads large binary files by reading a couple of&lt;br&gt;
&amp;gt; &amp;gt; numbers per time in a complicated loop using fread.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Since the function isn't too fast I thought I'd read the entire file&lt;br&gt;
&amp;gt; &amp;gt; at once as uint8 and then typecast byte by byte to the relevant&lt;br&gt;
&amp;gt; &amp;gt; datatypes.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Why doing byte-by-byte? Use typecast for the whole array. Many function Matlab function is not designed to called many times, but rather on an array. Example:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; b=zeros(8*1024^2,1,'uint8');&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tic;&lt;br&gt;
&amp;gt; d=typecast(b,'double');&lt;br&gt;
&amp;gt; toc % Elapsed time is 0.012408 seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tic;&lt;br&gt;
&amp;gt; % a already allocated&lt;br&gt;
&amp;gt; k=1;&lt;br&gt;
&amp;gt; for i=1:8:length(b)&lt;br&gt;
&amp;gt; &#160; &#160; d(k)=typecast(b(i+(0:7)),'double');&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; toc % 40 sec&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; To my surprise the typecast method is actually a _lot_ slower than&lt;br&gt;
&amp;gt; &amp;gt; consecutively using fread, on inspection the profiler shows that the&lt;br&gt;
&amp;gt; &amp;gt; typecast function is the main culprit.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; The function that serves the typecast.m function is a mex file called&lt;br&gt;
&amp;gt; &amp;gt; typecastc.mexw32 (on my 32 bit system that is). It seems to be a&lt;br&gt;
&amp;gt; &amp;gt; function which is compiled from an accompying c file that is located&lt;br&gt;
&amp;gt; &amp;gt; in the same private directory: typecastc.c&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; When I look at the c source file I see that the function itself&lt;br&gt;
&amp;gt; &amp;gt; doesn't do any typecasting, it just creates a relevant datatype with&lt;br&gt;
&amp;gt; &amp;gt; the mex mxCreateNumericMatrix and then leaves the actual typecasting&lt;br&gt;
&amp;gt; &amp;gt; to the mx libraries.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; So all in all the typecasting slowness seems a result of matlab's&lt;br&gt;
&amp;gt; &amp;gt; inability to deal with c-style pointers from m-files and the penalty&lt;br&gt;
&amp;gt; &amp;gt; for this is the mx library overhead.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; An odd situation.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Anyone have a better method (than the mathworks have ;) for reading/&lt;br&gt;
&amp;gt; &amp;gt; typecasting?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Alternatively use MEMMAPFILE to read binary data.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; or MEX it, but first try to use TYPECAST on the whole array.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Bruno- Hide quoted text -&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; - Show quoted text -&lt;br&gt;
&lt;br&gt;
Hi Bruno,&lt;br&gt;
Thanks for your reply.&lt;br&gt;
&lt;br&gt;
The trouble is that the file contains a number of different datatypes&lt;br&gt;
in nested structures so that it is not really possibly to cast the&lt;br&gt;
entire file or parts of it. I guess the same issue applies to using&lt;br&gt;
memmapfile and I doubt it is faster that way.&lt;br&gt;
&lt;br&gt;
I guess the fastest reading strategy would just to be to transfer the&lt;br&gt;
code to a C++ mex function and do all the reading/converting there and&lt;br&gt;
then transfer the whole lot back. I was just a bit surprised that&lt;br&gt;
something as elementary as a typecast would take so long in matlab.&lt;br&gt;
&lt;br&gt;
R</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 14:26:02 -0500</pubDate>
      <title>Re: matlab typecast function very slow</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831#691723</link>
      <author>James Tursa</author>
      <description>roger &amp;lt;northsolomonsea@gmail.com&amp;gt; wrote in message &amp;lt;05e24510-1633-4d87-9cbb-39230a6efeec@n35g2000yqm.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Since the function isn't too fast I thought I'd read the entire file&lt;br&gt;
&amp;gt; at once as uint8 and then typecast byte by byte to the relevant&lt;br&gt;
&amp;gt; datatypes.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To my surprise the typecast method is actually a _lot_ slower than&lt;br&gt;
&amp;gt; consecutively using fread, on inspection the profiler shows that the&lt;br&gt;
&amp;gt; typecast function is the main culprit.&lt;br&gt;
&lt;br&gt;
If you want a fast typecast function, use my FEX submission that can be found here:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/17476-typecast-c-mex-function&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/17476-typecast-c-mex-function&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
The MATLAB built-in typecast does a data copy to generate the result. My version of typecast does not do a data copy, but does a shared data copy, so it runs faster. The speed improvement will depend on the size of the data. My files are names typecast.m and typecast.c, but you could rename them to something else if desired.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 14:54:32 -0500</pubDate>
      <title>Re: matlab typecast function very slow</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831#691729</link>
      <author>roger</author>
      <description>On Nov 3, 3:26&#160;pm, &quot;James Tursa&quot;&lt;br&gt;
&amp;lt;aclassyguy_with_a_k_not_...@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; roger &amp;lt;northsolomon...@gmail.com&amp;gt; wrote in message &amp;lt;05e24510-1633-4d87-9cbb-39230a6ef...@n35g2000yqm.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Since the function isn't too fast I thought I'd read the entire file&lt;br&gt;
&amp;gt; &amp;gt; at once as uint8 and then typecast byte by byte to the relevant&lt;br&gt;
&amp;gt; &amp;gt; datatypes.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; To my surprise the typecast method is actually a _lot_ slower than&lt;br&gt;
&amp;gt; &amp;gt; consecutively using fread, on inspection the profiler shows that the&lt;br&gt;
&amp;gt; &amp;gt; typecast function is the main culprit.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; If you want a fast typecast function, use my FEX submission that can be found here:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/17476-typecast-c-...&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/17476-typecast-c-...&lt;/a&gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The MATLAB built-in typecast does a data copy to generate the result. My version of typecast does not do a data copy, but does a shared data copy, so it runs faster. The speed improvement will depend on the size of the data. My files are names typecast.m and typecast.c, but you could rename them to something else if desired.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&lt;br&gt;
James,&lt;br&gt;
Many thanks, although I'll probably write the whole reading/converting&lt;br&gt;
part in C++ and transfer the lot back to matlab via mx the shared data&lt;br&gt;
copy is very interesting!&lt;br&gt;
regards, R</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 16:31:01 -0500</pubDate>
      <title>Re: matlab typecast function very slow</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831#691755</link>
      <author>James Tursa</author>
      <description>roger &amp;lt;northsolomonsea@gmail.com&amp;gt; wrote in message &amp;lt;0598df9f-fdca-4bb3-bbb0-22b6ff33331c@a32g2000yqm.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ... although I'll probably write the whole reading/converting&lt;br&gt;
&amp;gt; part in C++ and transfer the lot back to matlab via mx the shared data&lt;br&gt;
&amp;gt; copy is very interesting!&lt;br&gt;
&lt;br&gt;
Tip if you go this route. First create your return variables with the mxCreate___ functions, then get the data pointers to them with the mxGetPr function. Then use *these* pointers in your reading. That way the data gets read directly into the mxArray variable that you want returned to MATLAB and you will avoid an extra data copy.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 16:54:58 -0500</pubDate>
      <title>Re: matlab typecast function very slow</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264831#691764</link>
      <author>roger</author>
      <description>On Nov 3, 5:31&#160;pm, &quot;James Tursa&quot;&lt;br&gt;
&amp;lt;aclassyguy_with_a_k_not_...@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; roger &amp;lt;northsolomon...@gmail.com&amp;gt; wrote in message &amp;lt;0598df9f-fdca-4bb3-bbb0-22b6ff333...@a32g2000yqm.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; ... although I'll probably write the whole reading/converting&lt;br&gt;
&amp;gt; &amp;gt; part in C++ and transfer the lot back to matlab via mx the shared data&lt;br&gt;
&amp;gt; &amp;gt; copy is very interesting!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Tip if you go this route. First create your return variables with the mxCreate___ functions, then get the data pointers to them with the mxGetPr function. Then use *these* pointers in your reading. That way the data gets read directly into the mxArray variable that you want returned to MATLAB and you will avoid an extra data copy.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; James Tursa&lt;br&gt;
&lt;br&gt;
Thanks.&lt;br&gt;
&lt;br&gt;
What I was thinking of is an approach that lets me extract/interpret&lt;br&gt;
data either directly from file or from a &quot;raw&quot; data array depending on&lt;br&gt;
available memory. If there's enough memory I can read everything at&lt;br&gt;
once into the raw data array and extract/intrepret from there, if&lt;br&gt;
there's not I'll have to read part by part from file which may be a&lt;br&gt;
bit slower. The extracted data will go directly into the relevant&lt;br&gt;
mxArray to avoid yet another copy of the data.&lt;br&gt;
&lt;br&gt;
In any case, it should be a lot faster than m-code, I've noticed in&lt;br&gt;
the past that especially loops are _much_ quicker in compiled C code.</description>
    </item>
  </channel>
</rss>

