Since you are only doing a merge, not a full sort, this is just a basic merge cycle. Purely serial input-output. No need to worry about buffers. Put the zipper on the jacket. It is so simple. (Note: this can be much faster if the numbers are in binary format in files. Not only will the files be smaller, but the program will be limited by I / O and the numbers will be absolutely accurate.)
double GetNumberFromFile(FILE file){ if (feof(file)){ return BIGBIGNUMBER; } else { return ReadADouble(file); } } double A = GetNumberFromFile(AFILE); double B = GetNumberFromFile(BFILE); while (A < BIGBIGNUMBER && B < BIGBIGNUMBER){ if (A < B){ write A; A = GetNumberFromFile(AFILE); } else if (B < A){ write B; B = GetNumberFromFile(BFILE); } else { write A; write B; // or not, if you want to eliminate duplicates A = GetNumberFromFile(AFILE); B = GetNumberFromFile(BFILE); } } while (A < BIGBIGNUMBER){ write A; A = GetNumberFromFile(AFILE); } while (B < BIGBIGNUMBER){ write B; B = GetNumberFromFile(BFILE); }
When answering your question, consider a simpler problem by copying one file to another. You only perform sequential I / O in which the file system is really good. You write a simple loop to read small units, such as bytes or int from a file, and write them to another. As soon as you try to read a byte, the system allocates a good large buffer, intercepts a large chunk of the file into the buffer, and then transfers the byte from the buffer to you. He continues to do this until you need another buffer, when he invisibly looks into another one for you. The same thing happens with the file you are writing. Now the processor is quite fast, so it can iterate over the input bytes, copying them at the output, for a fraction of the time spent reading or writing a buffer, because reading or writing cannot go faster than external equipment. The only reason a larger buffer will help is because part of the read / write time is called the so-called βlatencyβ, mainly the time it takes to move the head to the desired track and wait for the desired sector to appear. Most file systems break files into chunks that sprinkle around the disk, so the head jumps anyway. You can hear it.
The only difference between copying and a merge algorithm like yours is reading two files, not one. In any case, the base time sequence is a series of buffers that are read and written with a small amount of CPU activity. (It is possible to perform overlapping I / O operations, so that the CPU takes place during I / O, so basically there are no delays between reading the buffer and writing, but this was more significant when the processors were 1000 times slower.)
Of course, if you can arrange it so that all the files that have been read and written are on separate physical disks and the disks are not very fragmented, then the amount of head movement could be minimized, and larger buffers could help, But basically, with a simple program, you can pretty much expect that simple code will run as fast as a disk can move data, and giant buffers can help, but not so much.