Binary vs Alphanumeric Files – Why Binary Files Load Quicker

binarycdatafile handling

I've noticed that when I load/store large data files in a binary format, the program runs much faster than if I load data from an ASCII encoded file.

Why is this the case? The data in my case is plain, with limited parsing involved other than read() or fscanf().

Best Answer

Using fscanf() in itself probably explains most of it. fscanf() has to interpret the passed-in format string, and then has to scan the input stream from the file, trying to match the specified pattern. That's actually a huge amount of work. read() just has to read in the specified number of bytes from the file and doesn't have to do any parsing of the input. By contrast, fgets() does a little more work than read() since it has to watch for newlines, but it does a lot less work than fscanf().