Chapter 11. Optimize I/O
A program is a spell cast over a computer, turning input into error messages.
Anonymous
This chapter treats efficient use of the C++ streaming I/O functions for the commonly occurring example of reading and writing text data. Reading and writing data are activities so commonplace that developers donât even notice themâyet these activities are time-consuming.
The rotation of a disk platter is as ponderous and slow to todayâs ultra-fast computer chips as the rotation of the Earth is to us. The read heads have inertia that must be overcome to move their bulk from track to track. These physical properties fiercely resist attempts at improving hardware performance. In the internetworked world of limited data rates and busy servers, response latency may be measured in seconds rather than milliseconds. Even the speed of light becomes a significant factor when streaming data from distant computers.
Another problem with I/O is that there is a lot of code between the userâs program and the spinning platters of the disk or the network interface card. The cost of all this code must be managed to make I/O as efficient as possible.
A Recipe for Reading Files
There are a number of formulas on the Web for reading and writing files, and even some that claim to be fast. They range in performance over a whole order of magnitude in my tests. Readers who are already smart enough not to use an unvetted recipe from the Internet (such as the legendarily dangerous and ...
Get Optimized C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.