Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: matlab typecast function very slow
Date: Tue, 3 Nov 2009 14:26:02 +0000 (UTC)
Organization: Boeing
Lines: 17
Message-ID: <hcpehq$ct7$1@fred.mathworks.com>
References: <05e24510-1633-4d87-9cbb-39230a6efeec@n35g2000yqm.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257258362 13223 172.30.248.37 (3 Nov 2009 14:26:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 3 Nov 2009 14:26:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:582052


roger <northsolomonsea@gmail.com> wrote in message <05e24510-1633-4d87-9cbb-39230a6efeec@n35g2000yqm.googlegroups.com>...
> 
> Since the function isn't too fast I thought I'd read the entire file
> at once as uint8 and then typecast byte by byte to the relevant
> datatypes.
> 
> To my surprise the typecast method is actually a _lot_ slower than
> consecutively using fread, on inspection the profiler shows that the
> typecast function is the main culprit.

If you want a fast typecast function, use my FEX submission that can be found here:

http://www.mathworks.com/matlabcentral/fileexchange/17476-typecast-c-mex-function

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.

James Tursa