Are there generally accepted recommendations for achieving the fastest sequential file I / O in C ++?
Rule 0: Measurement. Use all available profiling tools and get to know them. It is almost a commandment in programming that if you haven’t measured it, you don’t know how fast it is, and for I / O this is even more true. Be sure to check in real working conditions, if possible. A process that does not have competition for the I / O system can be overly optimized, fine-tuned for conditions that do not exist under real loads.
Use mapped memory instead of writing to files. This is not always faster, but it allows optimizing I / O in the operating system, but is relatively portable, avoiding unnecessary copying and using the OS’s knowledge of how the disk is actually used. ("Portable" if you use a shell, not an API call for the OS).
Try and linearize your output as much as possible. The need to skip around memory to find buffers for writing can have noticeable effects under optimized conditions, because cache problems, paging, and other memory subsystems will start to matter. If you have a lot of buffers, pay attention to scatter I / O support, which is trying to do this linearization for you.
Some possible considerations:
- Recommendations for choosing the optimal buffer size
Page size for beginners, but be prepared to customize from there.
- Will a portable library, such as boost :: asio, be too abstract to reveal the complexities of a particular platform, or can they be considered optimal?
Do not consider it optimal. It depends on how carefully the library is loaded on your platform and how much effort the developers put into its rapid implementation. Having said that, a portable I / O library can be very fast, because most systems have fast abstractions, and you can usually come up with a common API that spans a lot of databases. Boost.Asio, as far as I know, is finely tuned for the specific platform on which it is located: there are a number of OS variants of specific APIs for fast asynchronous I / O (for example, epoll , / dev / epoll , kqueue , Windows, overlapping I / O ), and Asio wraps them all.
- Is asynchronous I / O always preferable to synchronous? What if the application is not connected to the CPU?
Asynchronous I / O is no faster than synchronous I / O. What asynchronous I / O does is that your code does not waste time waiting for I / O to complete. This happens faster than the other method, without wasting time using threads, because it will go into your code when I / O is ready, and not earlier. There are no false starts or downtime issues that need to be stopped.
quark Jul 29 '09 at 18:31 2009-07-29 18:31
source share