Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

fread vs. FSRead

Using CW 10 on a 604 based Mac-clone I have a C program that reads a binary file a few bytes at a time. The SIOUX implemented fread() function is almost 10 times faster than the Mac Toolbox function FSRead() at reading the whole file. I would have thought that fread() just calls FSRead() but apparently that's not the case. Is the problem just buffering? Does fread() have built in buffering that FSRead() lacks?
0
tlyons
Asked:
tlyons
1 Solution
 
lookerCommented:
Yes fread does use an internal buffer for caching file reads. I'm not sure exactly what size the CW10 buffer is but it will be a minimum of 512 bytes and probably 2K or 4K. This buffer is filled on the first fread call, and subsequent fread calls actuall just transfer memory from the buffer to your storage. When the buffer is exhausted, or you position to somewhere in the file outside the buffer, another low level (FSRead call is made to re-fill the buffer).

FSRead on the other hand usesthe volume buffer for each block read into memory before it transfers it to your storage.

You could drastically improve performace by reading block size chunks from disk and reading through that buffer on your own. There is some extra programming involved, but that would also allow you to implement asynchronous I/O to make your code even more efficient. (You can parse one buffer while reading in another from disk.)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now