If you really want to optimize this, you probably want to discard the C ++ fstream stuff, or at least disable its buffering. fstream does a lot of memory allocation, and freeing and buffering can be read in more data than necessary. The OS will most likely need to read a whole page to get the few bytes you need, but fstream will probably want it to copy at least as many (and possibly more that require more reads) to its buffers, which will take time.
Now we can move on to larger victories. You might want to use the IO OS routines directly. If you are using a POSIX system (such as Linux), then open , lseek , read and close well suited for this and may be required if you do not have the following system calls.
If all the files that you are trying to read from life in one directory (folder) or under one, you may find that opening the directory with opendir or open("directory_name", O_DIRECTORY) (depending on whether you need to read the directory entries yourself), and then calling openat , which takes a file descriptor for the directory entry, because one of its arguments will speed up the opening of each file, since the OS will not work as hard as looking for the file you are trying to open every time (this data is probably will be located in the OS file system cache, but all it takes time and lots of tests).
Then you can read the data using the pread system call without having to search for the data you need. pread accepts the offset rather than using the idea of the OS of the current search point. This saves you at least one system call.
Edit
If your system supports asynchronous I / O, this should speed things up, since you can continue to work and let the OS know what you need before you remove it (this allows the OS graphics to read the disk better, especially for spinning disks), but it can get complicated. This will probably save you a lot of time.
nategoose
source share