Calculate the ideal block size

I'm writing a program that has to write a list of files into a single file (kinda like an archive), with no compression, and write a list of the files at the top of the output file. That's all fine and it works.

I'm just wondering if anyone has an algorithm for calculating the ideal blocksize of the file. I was going to do something like:
1
2
3
4
5
6
7
8
9
10
block_size = MAXIMUM_BLOCK_SIZE
while ((file_length % block_size) && (block_size))
        block_size--;

block_count = file_size / block_size;

while (block_count--) {
        fread(buffer,  1, block_size, file);
        fwrite(buffer, 1, block_size, file);
}


is there a better way?
man stat wrote:
The st_blksize field gives the "preferred" blocksize for efficient file system I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)

Dunno if bigger is better.

No need for block_size to be a divisor of file_length. Just read the last one with something like istream::readsome (or calculate how much do you need)
Bigger would usually be faster because you'd do less reads and less writes, but obviously memory is a factor. You can't assume anything about the amount of memory someone has...

I'm using C file I/O, but I'm pretty sure fread/fwrite will read as much as it can, so block_size shouldn't need to be a divisor of file_length anyway.
Topic archived. No new replies allowed.