Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: fread unnecessarily slow in big-endian mode
Replies: 2   Last Post: Mar 18, 2014 6:13 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Yair Altman

Posts: 1,349
Registered: 3/19/07
Re: fread unnecessarily slow in big-endian mode
Posted: Mar 18, 2014 5:40 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"David Young" wrote in message <lga8as$2ee$1@newscl01ah.mathworks.com>...
> Timing shows that this
>
> fid = fopen('bigfile', 'r', 'b'); % reading in big-endian mode
> d1 = fread(fid, inf, '*uint32');
> fclose(fid);
>
> is much slower than this
>
> fid = fopen('bigfile', 'r', 'l'); % reading in little-endian mode
> d2 = swapbytes(fread(fid, inf, '*uint32')); % then swap afterwards
> fclose(fid);
>
> typically by a factor of 2 for a 0.5 GB file (taking minimum times over a number of trials). The effect seems to be even bigger for bigger files. The results d1 and d2 are, of course, identical. I'm using R2013b under 64-bit Windows.
>
> In practical terms, there's no problem: I just use the second method. (That's why I'm writing here, rather than on Answers.) But I'm puzzled as to how something as basic as fread can end up being less efficient than a call to another function, with the overhead of the intermediate array that that entails.
>
> I wondered if anyone can shed any light on this? If anyone from The MathWorks is watching, could you comment? Is it worth putting in a bug report?



I believe that this is not a bug, but an expected outcome: You are most probably running on a little-endian platform. In this case, running the vectorized swapbytes() function on the result is faster than using the non-default fread() endianness, which in most likelihood does the same thing numerous times internally. fread is a built-in function implemented in C, so swapping individual bytes in its main loop is very fast. But even so, a multi-threaded vectorized swapbytes() that is performed just once on the returned data, en-block, is more efficient than the single-threaded fread() loop.

Yair Altman
http://UndocumentedMatlab.com
Read my upcoming MATLAB Performance Tuning book (CRC Press, 2014)




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.