views:

96

answers:

2

Is there any noticeable difference in speed between these 2 scenarios?

Scenario 1: I have a file of size 1024 bytes filled with 0s for every byte. I open the file and write 1024 bytes of 1s with fwrite.

Scenario 2: I have a file of size 512 bytes filled with 0s for every byte. I open the file and write 1024 bytes of 1s with fwrite.

Obviously I end up with identical files, but is there any penalty in resizing the 2nd file on disk?

I'm trying to determine if resizing my files before using them will be of any worth. Bonus points to anyone who can show me some Linux documentation to prove this either way.

+6  A: 

This is naturally file system specific. but you can be pretty sure that growing a file can be more expensive than writing in a pre-allocated file because growing it involves file system inode allocations while writing to pre-allocated inodes does not.

How big this difference actually is depends on the file system, and on the how exactly you are using it. I guess in some cases it can be meaningful.

another issue to consider is fragmentation. you will get better read/write performance if your file is on a contiguous block (or several large blocks) on the disk. pre-allocating a file will give you much better chance that it will be less fragmented.

Omry
Almost never meaningful on Linux due to insane amounts of caching. But all your points are still spot on.
Matt Joiner
+4  A: 

If the sizes you are talking about are smaller than the minimum allocation size (which it appears to be) then it really won't be a measurable difference.

Would it be measurable for larger values? Probably.

Would it matter? You can only tell by profiling that it is a bottleneck for your application.

Amardeep
+1 for having pointed out the blindingly obvious: measure and see.
JUST MY correct OPINION