Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How efficient is reading a file one byte at a time in C?

Tags:

c

After going through most of the book, "The C Programming Language," I think I have a decent grasp on programming in C. One common C idiom presented in that book is reading a file a single byte at a time, using functions like getchar() and fgetc(). So far, I've been using these functions to do all IO in my C programs.

My question is, is this an efficient way of reading a file? Does a call to get a single byte require a lot of overhead that can be minimized if I read multiple bytes into a buffer at a time, for instance by using the read() system call on Unix systems? Or do the operating system and C library handle a buffer behind the scenes to make it more efficient? Also, does this work the same way for writing to files a single byte at a time?

I would like to know how this generally works in C, but if it is implementation or OS specific, I would like to know how it works in GCC on common Unix-like systems (like macOS and linux).

like image 627
username34123 Avatar asked Sep 01 '18 19:09

username34123


People also ask

How to read one byte at a time in C?

If you want to read one byte at a time, call fread(&buffer, 1, 1, file); (See fread).

How to read file faster in C?

You can read entire data files an order of magnitude or two faster this way. Allocate your array storage with malloc and then pass a pointer to that memory and the total number of bytes to be read to read. Then call read once to read the entire data file into memory.


1 Answers

Using getchar() etc is efficient because the standard I/O library uses buffering to read many bytes at once (saving them in a buffer) and doles them out one at a time when you call getchar().

Using read() to read a single byte at a time is much slower, typically, because it makes a full system call each time. It still isn't catastrophically slow, but it is nowhere near as fast as reading 512, or 4096, bytes into a buffer.

Those are broad, sweeping statements. There are many caveats that could be added, but they are a reasonable general outline of the performance of getchar(), etc.

like image 125
Jonathan Leffler Avatar answered Sep 24 '22 21:09

Jonathan Leffler