### Find Faster I/O Techniques

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. Because malloc uses extra storage as overhead, allocating storage in a multiple of the page size actually results in more pages being allocated than required.
  Instead, use DosAllocMem to allocate this storage for the buffer.
  Instead, you may want to investigate one of the Win32 mechanisms for managing memory (i.e. virtual memory, memory mapped files, or heaps).
- 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 ReadFile, 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.
- you perform frequent read or write operations on your temporary files, create them as memory files. I/O operations can be performed more quickly on memory files than on disk files. To use memory files, you must specify the /Sv+ compiler option.
- 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 complex formatting. For simpler formatting, such as string concatenation, use a more specific string function.
- When reading input, read in a whole line at once rather than one character at a time.

Overview of Optimization

Optimize Your Application

C Library Functions: Low-Level Input/Output