There are a number of ways to improve your program's performance
of input and output:
- Use binary streams instead of text streams. In binary streams,
data is not changed on input or output.
- Use the low-level I/O functions, such as open and close.
These functions are faster and more specific to the application than
the stream I/O functions like fopen and fclose.
You must provide your own buffering for the low-level functions.
- If you do your own I/O buffering, make the buffer a multiple of
4K, which is the size of a page.
- When reading input, read in a whole line at once rather than one
character at a time.
- If you know you have to process an entire file, determine the
size of the data to be read in, allocate a single buffer to read it
to, read the whole file into that buffer at once using read,
and then process the data in the buffer. This reduces disk I/O, provided
the file is not so big that excessive swapping will occur. Consider
using the mmap function to access the file.
- Instead of scanf and fscanf,
use fgets to read in a string, and then use one of atoi, atol, atof,
or _atold to convert it to the appropriate format.
- Use sprintf only for complicated formatting.
For simpler formatting, such as string concatenation, use a more specific
string function.